首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用vbscript遍历XML节点

使用vbscript遍历XML节点
EN

Stack Overflow用户
提问于 2014-10-07 16:54:03
回答 1查看 363关注 0票数 1

我有一个XML文件,我想遍历它并输出几个特定的属性。这是我第一次直接使用XML数据,但我认为这应该是直截了当的,但我还是失败了。

XML的简化版本如下所示--我已经从这个示例中删除了额外的属性。

我希望遍历XML,读取这些节点/属性,以便我的输出与我所收到的输出相同。然而,目前,我得到一个标题,然后所有的日期在一个长长的列表。我是否需要计算所有节点,然后按我的方式计算和输出每个结果?对我来说,这个例子似乎太复杂了。

代码语言:javascript
复制
<?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>

我的剧本

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-10-07 17:42:31

您的主For Each循环一直覆盖使用构建表的变量,但student_dates除外,您一直将其附加到表中。循环逻辑没有问题,HTML构建逻辑没有问题。

也许您应该将您的方法更改为不断地附加到相同的输出变量。下面的代码可以做到这一点,而且看上去更令人高兴。

代码语言:javascript
复制
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中,与许多其他语言一样,字符串是不可变的。很多事情都是缓慢的。

代码语言:javascript
复制
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转义函数。

代码语言:javascript
复制
Function HTMLEscape(s)
   HTMLEscape = Replace( _
                Replace( _
                Replace( _
                Replace( s, _
                    "&", "&amp;"  _
                ),  "<", "&lt;"   _
                ),  ">", "&gt;"   _
                ), """", "&quot;" _
                )
End Function

注意,您的测试If Not Node is Nothing Then测试是不必要的。在这一点上,Node永远不可能是Nothing

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26241198

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档