使用vbscript遍历XML节点

时间:2014-10-07 16:54:04

标签: xml xpath 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

1 个答案:

答案 0 :(得分:1)

您的主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, _
                    "&", "&amp;"  _
                ),  "<", "&lt;"   _
                ),  ">", "&gt;"   _
                ), """", "&quot;" _
                )
End Function

PS:请注意,您的测试If Not Node is Nothing Then测试是不必要的。 <{1}}此时永远不会Node