Jump to content
Sign in to follow this  
Deon

Read XML file

Recommended Posts

Deon

I have read the other threads about how to use _XMLGetAttrib, and have got the below script, but it always returns -1 leading me to think it's not calling the correct parameter properly

#include <_XMLDomWrapper.au3>
#include <File.au3>

$XMLfile = InputBox("XML File","What is the path to the XML file?")

Global $oXML = ObjCreate("Microsoft.XMLHTTP")
$oXML.Open("GET", "$XMLfile", 0)
$oXML.Send

Global $sFile = _TempFile(@TempDir, '~', '.xml')
FileWrite($sFile, $oXML.responseText)

If _XMLFileOpen($sFile) Then
    Local $sLvl = _XMLGetAttrib('./Devices/Printer/UPOSstat/Event/Parameter', 'HoursPoweredCount')
    ConsoleWrite($sLvl & @LF)
EndIf

FileDelete($sFile)

A snippet of the XML file I'm trying to read:

<Devices>
    <Device Name="Printer">
        <UPOSStat xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.nrf-arts.org/IXRetail/namespace/">
            <Event>
                <Parameter>
                    <Name>HoursPoweredCount</Name>
                    <Value>834</Value>
                </Parameter>
                <Parameter>...</Parameter>
                <Parameter>...</Parameter>
                <Parameter>...</Parameter>

 

Basically, I want to pass the URL of the XML file to the script, and it returns the value of HoursPoweredCount, but I don't think I'm naming the path properly in _XMLGetAttrib.

 

Cheers,
Deon.

Share this post


Link to post
Share on other sites
jdelaney

That's not an attribute, it's a node:

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("test.xml")
$oParameters = $oXML.SelectNodes("//Event/Parameter")
For $oParameter In $oParameters

    $oName = $oParameter.SelectSingleNode("./Name")
    If String($oName.text) = "HoursPoweredCount" Then
        $oValue = $oParameter.SelectSingleNode("./Value")
        ConsoleWrite(String($oValue.text) & @CRLF)
        Exit
    EndIf
Next

 

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.

Share this post


Link to post
Share on other sites
Deon

Thanks for that, looks great.. it's returning the value now.

One thing though, say I have the same parameter (HoursPoweredCount) for two different devices (in the example above Device name is 'printer'),  how can I find HoursPoweredCount for another device called 'screen'?

Share this post


Link to post
Share on other sites
jdelaney

You can do this...or loop through all Devices, and logically dig deeper as you find what you are looking for.

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("test.xml")

; $sWhatDoYouWantToGrab = "Printer"
$sWhatDoYouWantToGrab = "Screen"

$oParameters = $oXML.SelectNodes("//Device[@name='" & $sWhatDoYouWantToGrab & "']//Event/Parameter")
For $oParameter In $oParameters
    $oName = $oParameter.SelectSingleNode("./Name")
    If String($oName.text) = "HoursPoweredCount" Then
        $oValue = $oParameter.SelectSingleNode("./Value")
        ConsoleWrite(String($oValue.text) & @CRLF)
        Exit
    EndIf
Next

 

 

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.

Share this post


Link to post
Share on other sites
Deon

Thanks for the reply, but unfortunately the script isn't returning anything now :(

Even when I remove the variables and hard-code the data into the code like this, it still returns nothing:

#include <_XMLDomWrapper.au3>
#include <File.au3>

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("C:\temp\test.xml")

$oParameters = $oXML.SelectNodes("//Device[@name='Printer']//Event/Parameter")
For $oParameter In $oParameters
    $oName = $oParameter.SelectSingleNode("./Name")
    If String($oName.text) = "HoursPoweredCount" Then
        $oValue = $oParameter.SelectSingleNode("./Value")
        ConsoleWrite(String($oValue.text) & @CRLF)
        Exit
    EndIf
Next

 

Share this post


Link to post
Share on other sites
Kyan

SelectNodes is case sensitive, this should work alright ;)

Local $oParameters, $oName, $oValue
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load(@DesktopDir&"\test.xml")

$oParameters = $oXML.SelectNodes("//Device[@Name='Printer']//Event/Parameter")
For $oParameter In $oParameters
    $oName = $oParameter.SelectSingleNode("./Name")
    If String($oName.text) = "HoursPoweredCount" Then
        $oValue = $oParameter.SelectSingleNode("./Value")
        ConsoleWrite(String($oValue.text) & @CRLF)
        Exit
    EndIf
Next

EDIT: This page has good content about selectors, jdelaney awaken my curiosity by using [@AttributeName='value'] :)

Edited by Kyan

Heroes, there is no such thing

One day I'll discover what IE.au3 has of special for so many users using it.
C'mon there's InetRead and WinHTTP, way better
happy.png

Share this post


Link to post
Share on other sites
dynamitemedia
On 5/5/2015 at 2:18 AM, jdelaney said:

You can do this...or loop through all Devices, and logically dig deeper as you find what you are looking for.

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load("test.xml")

; $sWhatDoYouWantToGrab = "Printer"
$sWhatDoYouWantToGrab = "Screen"

$oParameters = $oXML.SelectNodes("//Device[@name='" & $sWhatDoYouWantToGrab & "']//Event/Parameter")
For $oParameter In $oParameters
    $oName = $oParameter.SelectSingleNode("./Name")
    If String($oName.text) = "HoursPoweredCount" Then
        $oValue = $oParameter.SelectSingleNode("./Value")
        ConsoleWrite(String($oValue.text) & @CRLF)
        Exit
    EndIf
Next

 

 

 

how can we get just count of how many   "screen" are in the XML file  

$sWhatDoYouWantToGrab = "Screen" 


 

Share this post


Link to post
Share on other sites
jdelaney

 

$oParameters = $oXML.SelectNodes("//Device[@name='" & $sWhatDoYouWantToGrab & "']")
ConsoleWrite($oParameters.count & @CRLF)

 

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.

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  

×