Jump to content

XML help


Go to solution Solved by AdamUL,

Recommended Posts

I didn't want to necropost here: '?do=embed' frameborder='0' data-embedContent>>

Using the XML wrapper in the link above (also attached here), how can I enumerate the names within a section in the XML file?

Lets say I want to extract all of the setting names inside the below XML, I try using the function _XMLGetAllAttrib but there is no result. I was expecting that $a_names would contain the attribute names.

#include <array.au3>
#include <XMLDomWrapper.au3>

Global Const $sConfigFile = @ScriptDir & "\test.xml"
If Not FileExists($sConfigFile) Then
    $sErrorMsg = "Configuration File FolderMenu.xml Does Not Exist." & @LF & "Default Configuration File Is Used." & @LF
    MsgBox(4096, "", $sErrorMsg)
EndIf
If _XMLFileOpen($sConfigFile) = -1 Then
    $sErrorMsg &= "Error opening " & $sConfigFile & "."
    MsgBox(4096, "", $sErrorMsg)
EndIf

Global $a_Names[1]
Global $a_Values[1]

$a_Return = _XMLGetAllAttrib("SysSettings/SystemSpecific/MODEL/NICPowerSettings", $a_Names, $a_Values, "Setting")

If @error = -1 Then
    MsgBox(4096, "Error", $a_Return)
Else
    _ArrayDisplay($a_Names, "Names")
    _ArrayDisplay($a_Values, "Values")
EndIf
<?xml version="1.0" encoding="UTF-8"?>
<SysSettings>
    <SystemSpecific>
        <MODEL>
            <NICPowerSettings>
                <Setting Name="PMARPffload" Value="*PMARPOffload,REG_SZ,0"></Setting>
                <Setting Name="PMNSOffload" Value="*PMNSOffload,REG_SZ,0"></Setting>
                <Setting Name="WakeOnMagicPacket" Value="*WakeOnMagicPacket,REG_SZ,0"></Setting>
                <Setting Name="WakeOnPattern" Value="*WakeOnPattern,REG_SZ,0"></Setting>
                <Setting Name="EEELinkAdvertisement" Value="EEELinkAdvertisement,REG_SZ,0"></Setting>
                <Setting Name="ReduceSpeedOnPowerDown" Value="ReduceSpeedOnPowerDown,REG_SZ,0"></Setting>
            </NICPowerSettings>
        </MODEL>
    </SystemSpecific>
    <Global>
    </Global>
</SysSettings>

Test.xml

test.au3

XMLDomWrapper.au3

Always carry a towel.

Link to comment
Share on other sites

  • Solution

Check your XPATH in the _XMLGetAllAttrib function call.  Try this.

/SysSettings/SystemSpecific/MODEL/NICPowerSettings/

You could also just go with this as well.  Just using the full XPATH without the last parameter.  

$a_Return = _XMLGetAllAttrib("/SysSettings/SystemSpecific/MODEL/NICPowerSettings/Setting", $a_Names, $a_Values)

Also, if you use Notepad++, download the XML Tools plug-in.  It is very helpful when working with XPATH queries on an XML file.  

 

Adam

Link to comment
Share on other sites

Thank you, AdamUL. The leading "/" was the key.

Now the return is populated but only by one record (the last one). Maybe I'm not understanding the function - I thought it would return an array of all the setting names.

But I can work with this, I can query the count of the child nodes, then parse each one in turn with_XMLGetAllAttribIndex and act on it. I will try to test this further tomorrow.

$a_Count = _XMLGetChildNodes("/SysSettings/SystemSpecific/MODEL/NICPowerSettings")
$Idx = $a_Count[0] -1
For $i= 1 to $a_Count[0]
    $a_Return = _XMLGetAllAttribIndex("/SysSettings/SystemSpecific/MODEL/NICPowerSettings/", $a_Names, $a_Values, $a_Count[$i], $Idx)
    _ArrayDisplay($a_Values)
    $Idx = ($Idx - 1)
Next

Always carry a towel.

Link to comment
Share on other sites

Your welcome, glad I could help.  I believe this function has some issues.  

This UDF needs a lot of updating, at least to me.  i am currently working on project that deals with a lot of XML files.  To make my work easier, I'm currently working on updating this UDF to make it possible to work with multiple XML files at the same time.  I'm also working on determining how the functions work, if they do what they say they will do, and determine if some are even needed.  For some things, it is easier to work with the DOM's properties and methods directly.  I plan on adding the updated UDF library to Example Scripts, when I finish updating it.   

Here is the DOM Reference, if you are interested.  

 

Adam

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