[英]Traverse XML nodes using vbscript
I've got an XML file that I'd like to traverse and output a couple of specific attributes. 我有一个要遍历并输出几个特定属性的XML文件。 This is my first time using using XML data directly but I think this should be straightforward and yet I am defeated. 这是我第一次直接使用XML数据,但是我认为这应该很简单,但是我被打败了。
A simplified version of the XML is like this - I've removed extra attributes from this example. XML的简化版本是这样的-我从此示例中删除了多余的属性。
I'd like to traverse down the XML reading these nodes/attributes so that my output is the same as what I have received. 我想遍历XML来读取这些节点/属性,以便我的输出与我收到的相同。 However currently I'm getting one header and then all the dates in a long list. 但是当前我得到一个标题,然后是一长串中的所有日期。 Do I need to count all the nodes and then work my way through counting and outputting each result? 我是否需要计算所有节点,然后按照自己的方式计算和输出每个结果? That seems overly complicated for this example to me. 对于我来说,这个例子似乎太复杂了。
<?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>
My Script 我的剧本
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
Your main For Each
loop keeps overwriting the variables you use the build the tables, with the exception of student_dates
, which you keep appending to. 您的主要For Each
循环会不断覆盖您在构建表时使用的变量,但您会不断附加到该表中的student_dates
除外。 Your loop logic is all right, your HTML building logic is not. 您的循环逻辑没事,HTML的构建逻辑就不行。
Maybe you should change your approach to constantly appending to the same output variable. 也许您应该更改将方法不断添加到同一输出变量的方法。 The following code does that and is a little more pleasing to look at. 下面的代码可以做到这一点,并且看起来更令人愉悦。
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
With a helper class StringBuffer
, which helps string building performance. 使用帮助器类StringBuffer
,可帮助提高字符串构建性能。 (In VBScript, like in many other languages, string are immutable. Concatenating them a lot is slow.) (在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
and the obligatory HTML escape function you really should always use when building HTML from strings. 以及在从字符串构建HTML时确实应该始终使用的强制性HTML转义功能。
Function HTMLEscape(s)
HTMLEscape = Replace( _
Replace( _
Replace( _
Replace( s, _
"&", "&" _
), "<", "<" _
), ">", ">" _
), """", """ _
)
End Function
PS: Note the your test If Not Node is Nothing Then
test is unnecessary. PS:请注意您的测试, If Not Node is Nothing Then
则不需要测试。 Node
can never be Nothing
at this point. Node
永远不会是Nothing
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.