Jump to content

XML - query attributes - get all


Recommended Posts

...grrr, I'm giving up.

I'm unable to get it working .....

Task:

Read a XMl file, get all attributes and the node name of it.

I removed all my "non-working-super-code" from what I got.

#include <Array.au3>
#include <_XMLDomWrapper.au3>

Global $debugging = True, $iRET, $configtype, $aChildNodes
Global $sXML = @ScriptDir & "\xml.xml"
Global $sXmlNS = '"http://www.myurl.com/InstallationManifest"'


Global $sXmlQuery = "/MyNS:project"
$iRET = _XMLFileOpen($sXML)
ConsoleWrite("$iRET = " & $iRET & @LF)
$objDoc.setProperty("SelectionNamespaces", 'xmlns:MyNS=' & $sXmlNS)
$aChildNodes = _XMLGetChildNodes($sXmlQuery)
If IsArray($aChildNodes) Then
    _ArrayDisplay($aChildNodes)
Else
    ConsoleWrite("Error, $aChildNodes = " & $aChildNodes & @LF)
EndIf
for $i = 1 to $aChildNodes[0]

    ConsoleWrite("$aChildNodes=:" & $aChildNodes[$i]&":"& @LF)
        ConsoleWrite(">>>>>>>>>>>>>>>>>"& @LF)
        ConsoleWrite(">>>>>>>>>>>>>>>>>$aChildNodes[1]=:" & $aChildNodes[1]&":"& @LF)
        local $sXmlQuery_AttributeName = "/MyNS:project/MyNS:"&$aChildNodes[$i]&""
        $Attribute_Name = _XMLGetAttrib($sXmlQuery_AttributeName,'Name')
        ConsoleWrite(">>>>>>>>>>>>>>>>>"&$Attribute_Name& @LF)
        $Attribute_Value = _XMLGetAttrib($sXmlQuery_AttributeName,'Value')

Next

The XML file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<project
        xmlns='http://www.myurl.com/InstallationManifest'
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.myurl.com/InstallationManifest/doc/InstallationManifest.xsd">
    <Option Name="name1" Value="value1" />
    <Option Name="name2" Value="value2"/>
    <Feature Name="name3" Value="value3"/>
    <Feature Name="name4" Value="value4"/>
    <Property Name="name5" Value="value5"/>
    <Property Name="name6" Value=""/>
</project>

What I want to get is - maybe something like an array:

1|Option|name1|value1|

2|Option|name2|value2|

3|Feature|name3|value3|

4|Feature|name4|value4|

...

I tried also to work with _XMLGetAllAttrib and _XMLGetAllAttribIndex, found also a same question in the XML wrapper examples....but with no answer  :-(

Anyone to help me?

 

Link to comment
Share on other sites

here ya go

#include <Array.au3>
$oXML = ObjCreate("Microsoft.XMLDOM")

If Not IsObj($oXML) Then
    MsgBox(0, "", "Unable to create COM session to XML.")
    Exit
EndIf

$oXML.Load(@DesktopDir & "\xml.xml")
;~ ConsoleWrite($oXML.xML)

$oParent = $oXML.selectSingleNode( '//project' )

Enum $iData_NodeName, $iData_AttributeName, $iData_AttributeValue, $iData_UBound

$aData = ""

For $oChild In $oParent.childNodes
    For $oAttribute In $oChild.attributes
        If IsArray($aData) Then
            ReDim $aData[UBound($aData)+1][$iData_UBound]
        Else
            Local $aData[1][$iData_UBound]
        EndIf
        $aData[UBound($aData)-1][$iData_NodeName]       = $oChild.nodename
        $aData[UBound($aData)-1][$iData_AttributeName]  = $oAttribute.nodename
        $aData[UBound($aData)-1][$iData_AttributeValue] = $oAttribute.text
        ConsoleWrite("NodeName=[" & $oChild.nodename & "]: AttributeName=[" & $oAttribute.nodename & "] AttributeValue=[" & $oAttribute.text & "]" & @CRLF)
    Next
Next

_ArrayDisplay($aData)

above was example of dynamic attribute name|value pairs...here is what will match your array output desired:

$aData = ""
For $oChild In $oParent.childNodes

    If IsArray($aData) Then
        ReDim $aData[UBound($aData)+1][$iData_UBound]
    Else
        Local $aData[1][$iData_UBound]
    EndIf
    $aData[UBound($aData)-1][$iData_NodeName]       = $oChild.nodename
    $aData[UBound($aData)-1][$iData_AttributeName]  = $oChild.getAttribute("Name")
    $aData[UBound($aData)-1][$iData_AttributeValue] = $oChild.getAttribute("Value")
    ConsoleWrite("NodeName=[" & $oChild.nodename & "]: AttributeNameValue=[" & $oChild.getAttribute("Name") & "] AttributeValue'sValue=[" & $oChild.getAttribute("Value") & "]" & @CRLF)

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

Thx, for the answer, but when I start your script :

...SIMPLE_XML_READ3.AU3 (18) : ==> The requested action with this object has failed.:
For $oChild In $oParent.childNodes
For $oChild In $oParent.childNodes^ ERROR

I'm using WIn7/64, is this something with the XMLDOM object or with the namespace of the XML itself?

Edited by Tankbuster
Link to comment
Share on other sites

.......mmmh how to say.....I'm a idot......

Yes I noticed your Desktop search so, I adopted it to @scriptdir....but in the meantime (for any reason) I renamed the XMl file  :mad2: :mad2:   so obvious it hurts

....now you see how confused I was in the end......I'm sorry about asking you........now I go to a corner and wait there until the end of day......

But on the other hand:

THANK, you ! It's working (as expected, because I found several posts from you on the forum about XML, so you won't fail on my simple one.........sry......)

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...