Jump to content
Sign in to follow this  
dickep

How To Traverse XML file

Recommended Posts

dickep

OK,  I got something from another question and have gotten it to work - somewhat.  I can get the first element and the child node data.  BUT, how do I get the rest?  How can I get all the ReadItemList child node values?Sample.xml

Here is my code:

$oXML = ObjCreate("Microsoft.XMLDOM")

$oXml.load('C:\Documents\Sample.xml')

$oNodes = $oXML.selectSingleNode("//ReadItemList")

For $oNode In $oNodes.childnodes
    ConsoleWrite('Loop val: ' & $oNode.nodeName & ' -- ' &$oNode.text & @CRLF)
Next

here is my xml file attached.  It is much larger but for this I thought a subset would suffice.

Thanks

E

 

Share this post


Link to post
Share on other sites
kylomas

dickep,

>This thread has more info.  See jdelaney's sig for the XMLDOM parser.

kylomas


Forum Rules         Procedure for posting code

"I like pigs.  Dogs look up to us.  Cats look down on us.  Pigs treat us as equals."

- Sir Winston Churchill

Share this post


Link to post
Share on other sites
dickep

OK, I tried something (using some of the linked information) and am getting closer.  However, not getting the last "layer".

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML2 = ObjCreate("Microsoft.XMLDOM")
$oXML3 = ObjCreate("Microsoft.XMLDOM")
 $oXml.load('C:Sample.xml')
$result = $oXML.selectNodes( '//ReadItemLists' )
For $Node In $result
 $oXML2.loadxml($Node.xml)
 $result2 = $oXML2.selectNodes( '//ReadItemList' )
 $iCounter = 1
 For $Node2 In $result2
  $children = $Node2.childnodes
  For $child in $children
   ConsoleWrite (  "GroupInstance=[" & $iCounter & "] childNode|value=[" & $child.nodename & "|" & $child.text & "]" &  @CRLF )
  Next
  $iCounter +=1
 Next
 ConsoleWrite ( @CRLF )
Next

I am still using the Sample.xml input file just need to get the next "ReadItem" data.

Thanks

E

P.S. not currently using $oXML3 as I was trying to figure out where to call it.

Edited by dickep

Share this post


Link to post
Share on other sites
jdelaney

Here you go...use relative searches, rather than separate instances of xmldom:

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load('Sample.xml')
$oLists = $oXML.selectNodes( '//ReadItemList' )
For $oList In $oLists
    ; if there is only ever 1 instance, no need to grab nodes...can instead selectSingleNode
    $oReadItems = $oList.selectNodes("./ReadItem")
    For $oReadItem In $oReadItems
        For $oChild In $oReadItem.childNodes
            ConsoleWrite("childNode|value=[" & $oChild.nodename & "|" & $oChild.text & "]" &  @CRLF )
        Next
        ConsoleWrite(@CRLF)
    Next
Next

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
dickep

jdelany, thank you. this works HOWEVER, not sure I understand where or what is meant by the comment "; if there is only ever 1 instance, no need to grab nodes...can instead selectSingleNode".  Can this work with the sample file that has single layer nodes mixed with multi-layer nodes?  Or .... well, not sure about how to get all the xml node data out properly using the Sample.xml file I have attached.

Thanks again

E

Share this post


Link to post
Share on other sites
jdelaney

As written above, it will grab all 'list' nodes, then loop through all instances of their ReadItem data.

If you want to grab all 'lists' nodes, and then loop through all their 'list' notdes, and then loop through all their 'readitem', do this:

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load('Sample.xml')
$oReadParentItemLists = $oXML.selectNodes( '//ReadItemLists' )
For $oReadParentItemList In $oReadParentItemLists
    ; if there is only ever 1 instance, no need to grab nodes...can instead selectSingleNode
    $oReadItemLists = $oReadParentItemList.selectNodes("./ReadItemList")
    For $oReadItemList In $oReadItemLists
        $oReadItems = $oReadItemList.selectNodes("./ReadItem")
        For $oReadItem In $oReadItems
            For $oChild In $oReadItem.childNodes
                ConsoleWrite("childNode|value=[" & $oChild.nodename & "|" & $oChild.text & "]" &  @CRLF )
            Next
            ConsoleWrite(@CRLF)
        Next
    Next
Next

Both will basically grab any amount of instances of lists|Items


IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
dickep

jdelany,

I tried the last sample but it still did not get the first 9 'lists' data.  Am I looking at this wrong?  Or is there something else that needs tweeked?  I am trying to understand the XML file format to help me be able to import these for a project at a later date.

Thanks

E

Share this post


Link to post
Share on other sites
jdelaney

Here, you should really grab the items you want, rather than loop through all:

$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load('Sample.xml')

$oReadParentItemLists = $oXML.selectNodes( '//ReadItemLists' )
For $oReadParentItemList In $oReadParentItemLists
    ; if there is only ever 1 instance, no need to grab nodes...can instead selectSingleNode
    $oReadItemLists = $oReadParentItemList.selectNodes("./ReadItemList")
    For $oReadItemList In $oReadItemLists

        ; grab the nodes you want...
        $oReadItemListID = $oReadItemList.selectSingleNode("./ReadItemListID")
        $oDescription = $oReadItemList.selectSingleNode("./Description")
        ConsoleWrite($oReadItemListID.Text & @CRLF)
        ConsoleWrite($oDescription.Text & @CRLF)

        $oReadItems = $oReadItemList.selectNodes("./ReadItem")
        For $oReadItem In $oReadItems

            ; grab the nodes you want
            $oItemDescription = $oReadItem.selectSingleNode("./Description")
            $oItemTIMTagNumber = $oReadItem.selectSingleNode("./TIMTagNumber")
            ConsoleWrite(@TAB & $oItemDescription.Text & @CRLF)
            ConsoleWrite(@TAB & $oItemTIMTagNumber.Text & @CRLF)
        Next
        ConsoleWrite(@CRLF)
    Next
Next

IEbyXPATH-Grab IE DOM objects by XPATH IEscriptRecord-Makings of an IE script recorder ExcelFromXML-Create Excel docs without excel installed GetAllWindowControls-Output all control data on a given window.

Share this post


Link to post
Share on other sites
Inververs

Global Const $NODE_TEXT = 3
$xDoc = ObjCreate("Msxml2.DOMDocument.6.0")
$xDoc.Async = False
$xDoc.Load('Sample.xml')
DisplayNode($xDoc.childNodes, 0)

Func DisplayNode($childNodes, $indent)
    $Indent = $Indent + 2
    For $xNode In $childNodes
        If $xNode.nodeType = $NODE_TEXT Then
            ConsoleWrite(StringFormat('%' & $Indent & 's', '') & $xNode.parentNode.nodeName & ': ' & $xNode.nodeValue & @LF)
        EndIf
        If $xNode.hasChildNodes Then
            DisplayNode($xNode.childNodes, $Indent)
        EndIf
    Next
EndFunc   ;==>DisplayNode

Share this post


Link to post
Share on other sites
dickep

Inververs,

Thanks, this does exactly what I had hoped for.  Now to digest what you did and how it works so I can use this sometime later - for other projects.

E

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×