6

I have a web service which returns XML string similar to the one below:

<?xml version="1.0"?>
<Result>
    <PersonID>991166187</PersonID>
    <AddressID>1303836</AddressID>
</Result>

I need a VBScript code that will allow me to retrieve the values for PersonID and AddressID. My specific question is how can I retrieve value for PersonID, i.e. 991166187, from the XML string in my original post.

In terms of what I have tried, I have the following code:

Dim doc
Dim xmlString
Dim nodes
Dim idArray

xmlString = "<?xml version="1.0"?><Result><PersonID>991166187</PersonID><AddressID>1303836</Address&#8204;&#8203;ID></Result>"

Set doc = CreateObject("MSXML2.DOMDocument")
doc.loadXML(xmlString)

'Set nodes = doc.selectNodes("Result/PersonID/AddressID")
nodes = doc.getElementsByTagName("PersonID")

For Each node In nodes
  WScript.Echo "Person ID: " & node.text
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Babs
  • 195
  • 2
  • 3
  • 8
  • Can you show what you have researched? Do you have a *specific* question? Can you show what you have tried and tell us what isn't working? – rory.ap Oct 17 '15 at 13:45
  • My specific question is how can I retrieve value for PersonID i.e. 991166187 from the xml string in my original post. In terms of what I have tried, I have the following code – Babs Oct 17 '15 at 13:49
  • My specific question is how can I retrieve value for PersonID i.e. 991166187 from the xml string in my original post. In terms of what I have tried, I have the following code: `Dim doc Dim xmlString Dim nodes Dim idArray xmlString="9911661871303836" Set doc = CreateObject("MSXML2.DOMDocument") doc.loadXML(xmlString) 'Set nodes = doc.selectNodes("Result/PersonID/AddressID") nodes = doc.getElementsByTagName("PersonID") For each node in nodes WSCript.Echo "Person ID: " & node.text ` – Babs Oct 17 '15 at 13:58
  • 2
    Please don't paste code in the comments area. Edit your question. Format the code so we can read it. – rory.ap Oct 17 '15 at 14:20
  • What have you found online? Have you tried searching for an answer before posting it here? – rory.ap Oct 17 '15 at 14:24

4 Answers4

7

Went through your code and there were a couple of things you need to edit:

  1. The XML string you get has double quotes in it. You cannot directly use those double quotes and save it into a string. You have two options here:
    1. create an XML file from the string and parse
    2. replace the double quotes with single quotes using Replace
  2. The getElementsByTagName line should have a Set in it, since the value returned is an object.

Used this code on my machine and it retrieved the desired output:

Dim doc 
Dim xmlString 
Dim nodes
xmlString = "<?xml version='1.0'?><Result><PersonID>991166187</PersonID><AddressID>1303836</AddressID></Result>"

Set doc = CreateObject("MSXML2.DOMDocument") 
doc.loadXML(xmlString) 
Set nodes = doc.getElementsByTagName("PersonID")

For Each node In nodes
  MsgBox "Person ID: " & node.text
Next
MsgBox "done"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
schizovivek
  • 83
  • 1
  • 1
  • 8
  • Escaping the double quotes (`"..."`) would work as well. – Ansgar Wiechers Oct 19 '15 at 11:57
  • Have spent hours trying to see how xmldom can load from variable. Everybody loads from files but when it comes from reading from an xml feed (rest call), that doesn't work. The loadXML method was what I was looking for. Thanks a bunch. Also, getElementsByTagName worked well in my case where I had only one node as a target. – nenea Sep 08 '17 at 14:43
3

An alternative to getElementsByTagName would be selectNodes with an XPath expression.

Set doc = CreateObject("MSXML2.DOMDocument")
doc.loadXML xmlString

If doc.parseError <> 0 Then
  WScript.Echo doc.parseError.reason
  WScript.Quit 1
End If

Set nodes = doc.selectNodes("/Result/PersonID")

For Each node In nodes
  WScript.Echo "Person ID: " & node.text
Next

Change /Result/PersonID to //PersonID if you don't know (or don't care about) the exact path to the PersonID child node(s). Also, beware that XPath expressions are case-sensitive.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
2

Please use this below code.

Dim oXML
Set oXML = CreateObject("Microsoft.XMLDOM")

'Load the XML file
oXML.Load("D:\Projects\QTP Project\TestAutomation\Extra\PersonID.xml")
'Loop through each nodes
For Each oChdNd In oXML.DocumentElement.ChildNodes
   WScript.Echo oChdNd.nodeName&":"&oChdNd.text&vbCrLf
Next
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Kapi
  • 43
  • 8
  • 1
    This will work only if the nodes in question are immediate children of the root node. Also, `Microsoft.XMLDOM` is outdated and should not be used anymore (use `Msxml2.DOMDocument` instead). – Ansgar Wiechers Oct 19 '15 at 11:50
-3

I would advise you to use vbs regexp for that. Please check this topic How to match it correct way?

Community
  • 1
  • 1
Alex
  • 193
  • 1
  • 3
  • 16
  • 1
    I love RegEx, but using it to parse XML when there are proper XML APIs available is definitely a no-no. – Sascha L. Oct 31 '17 at 13:27