我有一个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发布于 2014-10-07 17:42:31
您的主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注意,您的测试If Not Node is Nothing Then测试是不必要的。在这一点上,Node永远不可能是Nothing。
https://stackoverflow.com/questions/26241198
复制相似问题