For some reason I cannot make this work. I have the following XML with 2 registration nodes. All I need is to return a <serialnumber>
(which the script currently does) as variable value and all <id>, <qty>
for this <serialnumber>
.
I can get as far as the "serial number' (see the code below) but cannot seem to get the loop for the individual <module>
working in order to get all <id>, <qty>
. I am getting the Object doesn't support this property or method: 'ModuleList.length' response.
===================== XML ================
<root>
<registration>
<name>For Test</name>
<serialnumber>1234567890</serialnumber>
<modules>
<module>
<name>SERVER : A</name>
<id>15</id>
<qty>1</qty>
</module>
<module>
<name>SERVER : B</name>
<id>40</id>
<qty>1</qty>
</module>
</modules>
</registration>
<registration>
<name>For Test</name>
<serialnumber>0987654321</serialnumber>
<modules>
<module>
<name>SERVER : 1</name>
<id>15</id>
<qty>1</qty>
</module>
<module>
<name>SERVER : 2</name>
<id>40</id>
<qty>1</qty>
</module>
<module>
<name>SERVER : 3</name>
<id>15</id>
<qty>1</qty>
</module>
<module>
<name>SERVER : 4</name>
<id>40</id>
<qty>1</qty>
</module>
</modules>
</registration>
</root>
===================== VB Script ================
Set objXML = Server.CreateObject("Msxml2.DOMDocument")
objXML.LoadXml(xmlString)
Set Root = objXML.documentElement
Set registrationList = Root.getElementsByTagName("registration")
For i = 0 to registrationList.length -1
Set serialnumber = objXML.getElementsByTagName("serialnumber")(i)
Set ModuleList = Root.getElementsByTagName("modules")(i)
For x = 0 to ModuleList.length -1
Set module = objXML.getElementsByTagName("module")(x)
Response.Write module.text ' this is where I was expecting to stuff the array
Next
Response.Write serialnumber.text & " "
Next
Set objXML = Nothing
XML is structured data, so use a structured method (XPath) to work with it. Repeated/Nested getElementsByTagName()
loose the hierarchical relations. In Code:
Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
Dim sFSpec : sFSpec = goFS.GetAbsolutePathName("..\testdata\xml\so23686152.xml")
Dim objMSXML : Set objMSXML = CreateObject("Msxml2.DOMDocument")
objMSXML.setProperty "SelectionLanguage", "XPath"
objMSXML.async = False
objMSXML.load sFSpec
If 0 = objMSXML.parseError Then
Dim ndlReg : Set ndlReg = objMSXML.selectNodes("/root/registration")
Dim ndReg
For Each ndReg In ndlReg
WScript.Echo ndReg.selectSingleNode("serialnumber").text
Dim ndMod
For Each ndMod In ndReg.selectNodes("modules/module")
WScript.Echo " " _
, ndMod.firstChild.text _
, ndMod.selectSingleNode("id").text _
, ndMod.childNodes(2).text
Next
Next
Else
WScript.Echo objMSXML.parseError.reason
End If
output:
1234567890
SERVER : A 15 1
SERVER : B 40 1
0987654321
SERVER : 1 15 1
SERVER : 2 40 1
SERVER : 3 15 1
SERVER : 4 40 1
Why don't you use a regexp for this ?
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("test.xml", ForReading)
text = objFile.ReadAll
Dim serialNumber
Set objRE = New RegExp
objRE.pattern = "<serialnumber>([0-9]+)</serialnumber>"
Set matches = objRE.execute(text)
if matches.count > 0 then
serialNumber = matches.item(0).submatches.item(0)
end if
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.