经典ASP循环遍历XML以查找和显示一个节点的详细信息

时间:2015-05-28 12:21:00

标签: xml vbscript asp-classic

我有一个XML文件,其中包含有关各个用户的详细信息。当他们访问网页时,我想找到他们的节点并显示相关信息。我一直在尝试这里找到的几个示例代码,但有些方法或属性我不知道如何访问。

这是XML:

<?xml version="1.0" encoding="UTF-8"?>
<data>
  <user id="001" name="Maria">
    <godate>2015-01-01</godate>
    <serials>
      <serial>AA-12345-1</serial>
    </serials>
  </user>
  <user id="002" name="Domingo">
    <godate>2015-02-02</godate>
    <serials>
      <serial>BB-12345-1</serial>
      <serial>BB-12345-2</serial>
    </serials>
  </user>
  <user id="003" name="Michael">
    <godate>2015-03-03</godate>
    <serials>
      <serial>CC-12345-1</serial>
      <serial>CC-12345-2</serial>
      <serial>CC-12345-3</serial>
    </serials>
  </user>
</data>

我需要根据ID属性找到一个节点,然后显示godate的值和它们的序列号的值(1到n)。因此,对于Domingo,输出应显示:

开始日期:2015-02-02
序列号1:BB-12345-1
序列号2:BB-12345-2

到目前为止,我最接近的是显示所有godate值和所有序列值,但我不知道如何将结果限制为一个用户(基于ID)并且只显示那个用户的godate和连续剧。

这是我到目前为止的ASP代码:

<%
    Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
    objXMLDoc.async = False 
    objXMLDoc.load(Server.MapPath("data.xml"))

    Set Root = objXMLDoc.documentElement
    Set NodeList = Root.getElementsByTagName("user")

    For i = 0 to NodeList.length -1 
        Set GoDate = objXMLDoc.getElementsByTagName("godate")(i)
        response.write "Go Date: " & godate.text & "<br>"
        Set Serials = objXMLDoc.getElementsByTagName("serials")(i)
        response.write "Serials: " & serials.text & "<br>"
    Next
%>

如何找到ID属性,然后才获取该节点的数据?

2 个答案:

答案 0 :(得分:3)

使用XPath

Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.async = False 
objXMLDoc.load(Server.MapPath("data.xml"))
objXMLDoc.setProperty "SelectionLanguage", "XPath"

Dim id 
id = "001"
Dim user
Set user = objXMLDoc.selectSingleNode("//user[@id = '" & id & "']")
goDate = user.selectSingleNode("godate").text
'output goDate here
For Each serial In user.selectNodes("serials/serial")
  s = serial.text
  'output s here
Next

答案 1 :(得分:1)

使用docs根据here中的骨架通过此演示代码工作:

Option Explicit

Dim oFS    : Set oFS   = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec    = oFS.GetAbsolutePathName(".\30506444.xml")
Dim oXDoc  : Set oXDoc = CreateObject("MSXML2.DomDocument.6.0")
oXDoc.setProperty "SelectionLanguage", "XPath"
oXDoc.async = False
oXDoc.load sFSpec

If 0 = oXDoc.ParseError Then
   WScript.Echo sFSpec, "looks ok"
   Dim sXPath
   For Each sXpath In Array( _
       "/data/user[@name='Maria']" _
     , "/data/user[@name='Michael']" _
   )
       Dim ndFnd : Set ndFnd = oXDoc.selectSingleNode(sXpath)
       If Not ndFnd Is Nothing Then
          WScript.Echo "found |" & ndFnd.xml & "|"
          WScript.Echo "extracted:"
          WScript.Echo "     Name:", ndFnd.getAttribute("name")
          WScript.Echo "  Go Date:", ndFnd.selectSingleNode("godate").text
          WScript.Echo "  Serials:"
          Dim i : i = 1
          Dim ndSerial
          For Each ndSerial In ndFnd.selectSingleNode("serials").childNodes
              WScript.Echo "  Serial" & i & ":", ndSerial.text
              i = i + 1
          Next
       Else
          WScript.Echo "not found |" & sXPath & "|"
       End If
   Next
Else
   WScript.Echo oXDoc.ParseError.Reason
End If

输出:

cscript 30506444.vbs
E:\trials\SoTrials\answers\tmp\30506444.xml looks ok
found |<user id="001" name="Maria">
        <godate>2015-01-01</godate>
        <serials>
                <serial>AA-12345-1</serial>
        </serials>
</user>|
extracted:
     Name: Maria
  Go Date: 2015-01-01
  Serials:
  Serial1: AA-12345-1
found |<user id="003" name="Michael">
        <godate>2015-03-03</godate>
        <serials>
                <serial>CC-12345-1</serial>
                <serial>CC-12345-2</serial>
                <serial>CC-12345-3</serial>
        </serials>
</user>|
extracted:
     Name: Michael
  Go Date: 2015-03-03
  Serials:
  Serial1: CC-12345-1
  Serial2: CC-12345-2
  Serial3: CC-12345-3