2

I have an XML file containing details about individual users. When they visit a web page, I want to find their node and display the relevant information. I've been trying several example codes found here but there are some methods or properties I don't know how to access.

This is the 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>

I need to find a node based on the ID attribute, and then display the value of the godate and the values (1 to n) of their serials. So, for Domingo the output should show:

Go Date: 2015-02-02
Serial 1: BB-12345-1
Serial 2: BB-12345-2

So far the closest I've been able to come is to display all the godate values and all the serial values, but I don't know how to limit the results to one user (based on ID) and only show the godate and serials for that one user.

This is the ASP code I have so far:

<%
    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
%>

How can I find the ID attribute and then only get the data for that node?

John Cherry
  • 189
  • 1
  • 2
  • 10

2 Answers2

4

Use 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
Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
1

Use the docs to work thru this demo code based on the skeleton from 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

output:

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
Community
  • 1
  • 1
Ekkehard.Horner
  • 38,498
  • 2
  • 45
  • 96
  • Thanks very much! For now I am going to use the simpler solution provided above by Martin, but I am going to leverage your solution as well eventually; there is a lot of interesting stuff going on in this script that I can learn from and use. Greatly appreciate your help! – John Cherry May 28 '15 at 14:36