Deon Posted May 5, 2015 Posted May 5, 2015 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.
jdelaney Posted May 5, 2015 Posted May 5, 2015 (edited) 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 May 5, 2015 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.
Deon Posted May 5, 2015 Author Posted May 5, 2015 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'?
jdelaney Posted May 5, 2015 Posted May 5, 2015 (edited) 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 May 5, 2015 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.
Deon Posted May 5, 2015 Author Posted May 5, 2015 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
Kyan Posted May 5, 2015 Posted May 5, 2015 (edited) 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 NextEDIT: This page has good content about selectors, jdelaney awaken my curiosity by using [@AttributeName='value'] Edited May 5, 2015 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
dynamitemedia Posted January 23, 2016 Posted January 23, 2016 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"
jdelaney Posted January 23, 2016 Posted January 23, 2016 (edited) $oParameters = $oXML.SelectNodes("//Device[@name='" & $sWhatDoYouWantToGrab & "']") ConsoleWrite($oParameters.count & @CRLF) Edited January 23, 2016 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.
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now