2

I'm trying to parse through the a XML tree in VB Script, but I'm unable to pull out the values I want. I'm would like to pull the name value (eg. CHNG_REQT_NB and CHNG_REQT_DTL_CO), however with the code I have below, I'm only able to get it to print out "column" and value associated with each, For example, with the below code and XML, it would print out as below:

column
13389
column
/* NOTES */ 1) CR#145: New Code Table 

I would like it to print as

CHNG_REQT_NB
13389
CHNG_REQT_DTL_CO
/* NOTES */ 1) CR#145: New Code Table

Could someone please point out where I'm going wrong? Below are my code and XML samples.

Set objXMLDoc = CreateObject("Microsoft.XMLDOM") 
objXMLDoc.Async = False 
objXMLDoc.Load("result.xml")

Set Root = objXMLDoc.DocumentElement 
Set rowList = Root.GetElementsByTagName("row")

For Each row In rowList
     handleNode(row)
Next

Sub handleNode(row)
    For Each elem In row.ChildNodes
        WScript.Echo elem.TagName
        WScript.Echo elem.Text
    Next
End Sub
<?xml version="1.0" encoding="UTF-8"?>
<resultsets>
<resultset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<row>
<column name="CHNG_REQT_NB">13389</column>
<column name="CHNG_REQT_DTL_CO">/* NOTES */ 1) CR#145: New Code Table</column>
</row>
</resultset>
</resultsets>
2
  • 3
    WScript.Echo elem.tagName -> WScript.Echo elem.GetAttribute("name") Commented Aug 15, 2017 at 14:28
  • That was it! I've tried that but I must have tried it in the wrong spot or something. Post it as the answer and I'll accept it. Commented Aug 15, 2017 at 14:32

2 Answers 2

2

You need to get the value of the name attribute of the nodes rather than the node names.

Change

For Each elem In row.ChildNodes
    WScript.Echo elem.TagName
    WScript.Echo elem.Text
Next

into

For Each elem In row.ChildNodes
    WScript.Echo elem.GetAttribute("name")
    WScript.Echo elem.Text
Next

and the code will do what you want.

Sign up to request clarification or add additional context in comments.

Comments

2

Use XPath for this, don't mess around crawling through child nodes.

Set xml = CreateObject("Msxml2.DOMDocument")
xml.Async = "False"

xml.Load("45694693.xml")

if( xml.parseError ) then  
    WScript.Echo        xml.parseError.reason & " line: " & xml.parseError.line
end if

for each ndColumn in xml.selectNodes( "//column" )
    WScript.Echo ndColumn.getAttribute( "name" ) + vbCrLf + ndColumn.text
next

WScript.Echo strReturn 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.