Jump to content

Using XMLDomWrapper.au3 to Get Attributes


Recommended Posts

Hi,

I've been looking at XPath tutorials and trying to find Autoit example to solve a simple problem, I must be missing something here and would be grateful for help.

Given:

#include <_XMLDomWrapper.au3>

_XMLFileOpen("c:\xmltest1.xml")

local $ids =_XMLGetAttrib('/r/line', "b")
ConsoleWrite(@LF & "=====" & $ids & "=====" & @LF)

;~ <r>
;~  <line a="1" b="2">hi</line>
;~  <line a="3" b="4">hey</line>
;~  <line a="5" b="6">hello</line>
;~ </r>

and console says: =====6=====

But I want to get all values for "b=" I want to get from the XML file 2,4,6

I tried the _XMLGetAllAttrib function but if that's the way to do it then I'm doing something wrong too. I've tested the XPath:  /r/line/@b  in Notepad++ XML Plugin and this XPath gives all the b values. If anyone could enlighten me I would appreciate it. Thanks. How do I get all the b values out of the XML?

 

Link to comment
Share on other sites

Hi,

I know you are wanting to do this with XPATH (Maybe someone can help with this), I have tried with XMLDomWrapper and had no luck. But for the sake of a quick answer to your problem. You could try the below method, Returns an Array with Results.

#include <Array.au3>

$vXML = '<r>'
$vXML &=  '<line a="1" b="2">hi</line>'
$vXML &=  '<line a="3" b="4">hey</line>'
$vXML &=  '<line a="5" b="6">hello</line>'
$vXML &= '</r>'

$vRequest = StringRegExp($vXML, '<line\sa=".*?"\sb="(.*?)">.*?</line>', 3)

_ArrayDisplay($vRequest)

Also managed to get it working by declaring the XMLDOM Object

#include <Array.au3>

$vXML = '<r>'
$vXML &=  '<line a="1" b="2">hi</line>'
$vXML &=  '<line a="3" b="4">hey</line>'
$vXML &=  '<line a="5" b="6">hello</line>'
$vXML &= '</r>'

Local $vXPath = '//r/line'
Local $vAttrib = 'b'

$vXMLObj = ObjCreate('Microsoft.XMLDOM')
$vXMLBody = $vXMLObj.loadXML($vXML)
$vXMLNodeList = $vXMLObj.selectNodes($vXPath)
$vXMLNodeLength = $vXMLNodeList.length

Local $vArr[$vXMLNodeLength]

    For $i = 0 To $vXMLNodeList.length - 1
        $vGetAttrib = $vXMLNodeList.item($i).getAttribute($vAttrib)
        $vArr[$i] = $vGetAttrib
    Next


_ArrayDisplay($vArr)





Edited by mrflibblehat

[font="'courier new', courier, monospace;"]Pastebin UDF | Prowl UDF[/font]

Link to comment
Share on other sites

awww, beat me to it...posting anyways to show collection loops (not storing in an array)

$sXML = '<r><line a="1" b="2">hi</line><line a="3" b="4">hey</line><line a="5" b="6">hello</line></r>'
$oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.LoadXML($sXML)
$oAttribCol = $oXML.selectNodes( '//line/@b' )

For $oAttrib In $oAttribCol
    ConsoleWrite($oAttrib.value & @CRLF)
Next

ConsoleWrite(@CRLF & "Take 2" & @CRLF)

$oNodeCol = $oXML.selectNodes( '//line' )
For $oNode In $oNodeCol
    ConsoleWrite($oNode.getAttribute("b") & @CRLF)
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.
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

×
×
  • Create New...