Sign in to follow this  
Followers 0
DeDee

How to retrieve all XPaths from an XML file

9 posts in this topic

Hi

How can I get all XPaths from an XML file - (any xml file and any xpath)? I see that all functions for XMLs will retrieve data based on XPaths but I did not see any example for getting the XPaths.

What would be the best approach to to this?

Thank you,

Share this post


Link to post
Share on other sites



Did you check the >XMLDOM UDF for a suitable function?


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Yes, that was the first thing to check. But, as I said, the functions use XPath as input data, not return data. 

Share this post


Link to post
Share on other sites

I see. It wasn't clear to me what you have tried by just writing: "I see that all functions for XMLs ...".


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Use Notepad++, there is a plug-in called XML Tools.  There is a command "Current XML Path," that will return the XPATH for the node that the cursor is located in.  I use this when I'm writing code to work with XML files.  

 

Adam

Share this post


Link to post
Share on other sites

Thanks for the replies. I guess I was not that clear in my initial description  - I need to write an autoit script that will return me the XPaths (to attributes and nodes) from an xml. The script should be able to parse any xml file and send all the XPaths.  

Thanks

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

You'll need to construct them yourself, by recursively traveling inward through the XML.  I'd use _arrayadd, and pass the array through the function, to continually add to it.  Sounds fun.

Use .childNodes, .nodeName, .attributes, etc. (microsoft.xmldom)

Example:

$oXML = ObjCreate("Microsoft.XMLDOM")
$sXML_File = @DesktopDir & "\output.xml"
$oXML.load($sXML_File)

$root = $oXML.documentelement
Global $aXpaths[1]=["\" & $root.nodename]
RecursiveXPath($root,"\" & $root.nodename)
_ArrayDisplay($aXpaths)

Func RecursiveXPath($oCurrentParentNode,$sCurrentParentNodeXPath)
    $oAtts = $oCurrentParentNode.attributes
    For $oAtt In $oAtts
        _ArrayAdd($aXpaths,$sCurrentParentNodeXPath & "[@" & $oAtt.NodeName & "=" & $oAtt.NodeValue & "]" )
    Next
    $oNodes = $oCurrentParentNode.childNodes
    For $oNode In $oNodes
        If $oNode.nodename = "#text" Then ContinueLoop
        _ArrayAdd($aXpaths,$sCurrentParentNodeXPath & "\" & $oNode.nodename )
        RecursiveXPath($oNode,$sCurrentParentNodeXPath & "\" & $oNode.nodename)
    Next
EndFunc

I used a simple xml (an am not exporting the node.text, which would be an easy addition that you can do...

output example:

[0]|employees
[1]|employees[@version=1.0]
[2]|employeesemployee
[3]|employeesemployee[@something=123]
[4]|employeesemployeeperson
[5]|employeesemployeepersonfirstname
[6]|employeesemployeepersonfirstname[@something=124]
[7]|employeesemployeepersonlastname
[8]|employeesemployeepersonlastname[@something=125]
[9]|employeesemployee
[10]|employeesemployeeperson
[11]|employeesemployeepersonfirstname
[12]|employeesemployeepersonfirstname[@something=126]
[13]|employeesemployeepersonlastname

<employees version='1.0'>
    <employee something="123">
        <person>
            <firstname something="124">Tobias</firstname>
            <lastname something="125">Weltner</lastname>
        </person>
    </employee>
    <employee>
        <person>
            <firstname something="126">Two</firstname>
            <lastname>Three</lastname>
        </person>
    </employee>
</employees>

 

Edited by jdelaney
1 person likes this

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

With text

$oXML = ObjCreate("Microsoft.XMLDOM")
$sXML_File = @DesktopDir & "\output.xml"
$oXML.load($sXML_File)

$root = $oXML.documentelement
Global $aXpaths[1]=["\" & $root.nodename]
RecursiveXPath($root,"\" & $root.nodename)
_ArrayDisplay($aXpaths)

Func RecursiveXPath($oCurrentParentNode,$sCurrentParentNodeXPath)
    $oAtts = $oCurrentParentNode.attributes
    For $oAtt In $oAtts
        _ArrayAdd($aXpaths,$sCurrentParentNodeXPath & "[@" & $oAtt.NodeName & "=" & $oAtt.NodeValue & "]" )
    Next
    $oNodes = $oCurrentParentNode.childNodes
    For $oNode In $oNodes
        If $oNode.nodename = "#text" Then ContinueLoop
        $sText = XML_GetTextNodeValue($oNode)
        $xpath = $sCurrentParentNodeXPath & "\" & $oNode.nodename
        If StringLen ($sText) > 0 Then
            $xpath &= "[.=" & $sText & "]"
        EndIf
        _ArrayAdd($aXpaths,$xpath)
        RecursiveXPath($oNode,$sCurrentParentNodeXPath & "\" & $oNode.nodename)
    Next
EndFunc

Func XML_GetTextNodeValue($oCallersNode)
    Local $Text=""
    For $oChild In $oCallersNode.childNodes
        If Int($oChild.nodeType)==3 Then
            If StringLen($oChild.nodeValue) > 0 Then
                ConsoleWrite($oChild.nodeValue & @CRLF)
                Return String($oChild.nodeValue)
            EndIf
        EndIf
    Next
    Return $Text
EndFunc
1 person likes this

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

Thank you, jdelaney

The script works perfectly. :thumbsup:

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  
Followers 0