[英]Traverse XML nodes using vbscript
我有一个要遍历并输出几个特定属性的XML文件。 这是我第一次直接使用XML数据,但是我认为这应该很简单,但是我被打败了。
XML的简化版本是这样的-我从此示例中删除了多余的属性。
我想遍历XML来读取这些节点/属性,以便我的输出与我收到的相同。 但是当前我得到一个标题,然后是一长串中的所有日期。 我是否需要计算所有节点,然后按照自己的方式计算和输出每个结果? 对于我来说,这个例子似乎太复杂了。
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<contact_list >
<contact >
<enrolment description="Online Course April 14" >
<student_teaching_day teaching_date="16/04/14" teaching_days="1" session_from="9:01" session_to="10:01" />
<student_teaching_day teaching_date="24/04/14" teaching_days="1" session_from="9:02" session_to="10:02" />
<student_teaching_day teaching_date="01/05/14" teaching_days="1" session_from="9:03" session_to="10:03" />
<student_teaching_day teaching_date="08/05/14" teaching_days="1" session_from="9:03" session_to="10:03" />
</enrolment>
<enrolment description="Online Course May 14" >
<student_teaching_day teaching_date="16/04/14" teaching_days="1" session_from="9:01" session_to="10:01" />
<student_teaching_day teaching_date="24/04/14" teaching_days="1" session_from="9:02" session_to="10:02" />
<student_teaching_day teaching_date="01/05/14" teaching_days="1" session_from="9:03" session_to="10:03" />
<student_teaching_day teaching_date="08/05/14" teaching_days="1" session_from="9:03" session_to="10:03" />
</enrolment>
<enrolment description="Online Course June 14" >
<student_teaching_day teaching_date="16/04/14" teaching_days="1" session_from="9:01" session_to="10:01" />
<student_teaching_day teaching_date="24/04/14" teaching_days="1" session_from="9:02" session_to="10:02" />
<student_teaching_day teaching_date="01/05/14" teaching_days="1" session_from="9:03" session_to="10:03" />
<student_teaching_day teaching_date="08/05/14" teaching_days="1" session_from="9:03" session_to="10:03" />
</enrolment>
</contact>
</contact_list>
我的剧本
For Each Node In xmlDoc.documentelement.selectNodes("//enrolment")
If Not Node is Nothing Then
course_description = Node.getAttribute("description")
table_teaching_dates_start = "<table><tr><th colspan='4'><strong>"+course_description+"</strong></th></tr>"
For Each Day In Node.selectNodes("./student_teaching_day")
student_teaching_date = "<td>"+Day.getAttribute("teaching_date")+"</td>"
session_from = "<td>"+Day.getAttribute("session_from")+"</td>" + "<td> - </td>"
session_to = "<td>"+Day.getAttribute("session_to")+"</td>"
student_dates = student_dates + "<tr>" +student_teaching_date + session_from + session_to + "</tr>"
Next
table_teaching_dates_end ="</table>"
End If
Next
total = table_teaching_dates_start + table_row_start + student_dates + table_row_end + table_teaching_dates_end
您的主要For Each
循环会不断覆盖您在构建表时使用的变量,但您会不断附加到该表中的student_dates
除外。 您的循环逻辑没事,HTML的构建逻辑就不行。
也许您应该更改将方法不断添加到同一输出变量的方法。 下面的代码可以做到这一点,并且看起来更令人愉悦。
Dim enrolment, day, output, html
Set output = New StringBuffer
For Each enrolment In xmlDoc.selectNodes("//enrolment")
output.Append "<table>"
output.Append "<tr><th colspan='3'><strong>"
output.Append HTMLEscape(enrolment.getAttribute("description"))
output.Append "</strong></th></tr>"
For Each day In enrolment.selectNodes("./student_teaching_day")
output.Append "<tr>"
output.Append "<td>"
output.Append HTMLEscape(day.getAttribute("teaching_date"))
output.Append "</td>"
output.Append "<td>"
output.Append HTMLEscape(day.getAttribute("session_from"))
output.Append "</td>"
output.Append "<td>"
output.Append HTMLEscape(day.getAttribute("session_to"))
output.Append "</td>"
output.Append "</tr>"
Next
output.Append "</table>"
Next
html = output.ToString
使用帮助器类StringBuffer
,可帮助提高字符串构建性能。 (在VBScript中,就像许多其他语言一样,字符串是不可变的。将它们串联很多很慢。)
Class StringBuffer
Dim dict
Sub Class_Initialize
Set dict = CreateObject("Scripting.Dictionary")
End Sub
Sub Append(s)
dict.Add dict.Count, s
End Sub
Function ToString
ToString = Join(dict.Items, "")
End Function
Sub Class_Terminate
Set dict = Nothing
End Sub
End Class
以及在从字符串构建HTML时确实应该始终使用的强制性HTML转义功能。
Function HTMLEscape(s)
HTMLEscape = Replace( _
Replace( _
Replace( _
Replace( s, _
"&", "&" _
), "<", "<" _
), ">", ">" _
), """", """ _
)
End Function
PS:请注意您的测试, If Not Node is Nothing Then
则不需要测试。 Node
永远不会是Nothing
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.