Jump to content

How to retrieve all XPaths from an XML file


 Share

Recommended Posts

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,

Link to comment
Share on other sites

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

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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 2024-07-28 - Version 1.6.3.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
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

Link to comment
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

Link to comment
Share on other sites

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
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.
Link to comment
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
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.
Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...