Jump to content
Sign in to follow this  
ModemJunki

XML help

Recommended Posts

ModemJunki

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

Share this post


Link to post
Share on other sites
AdamUL

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

Share this post


Link to post
Share on other sites
ModemJunki

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

Share this post


Link to post
Share on other sites
AdamUL

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

Share this post


Link to post
Share on other sites
ModemJunki

I would be very interested to see an updated version of this UDF. Unfortunately I'm still something of an amateur and so can't really be of any help.

Share this post


Link to post
Share on other sites
Iczer

Here is the DOM Reference, if you are interested. 

 

i would really appreciate *.chm version of this manual... maybe someone can provide it?..

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  

  • Similar Content

    • Subz
      By Subz
      Can anyone assist with creating/modify/delete XML Nodes/Child Nodes?
      Have a document for example:
      <ConfigData> <parameter> <name>Setting One</name> <value>10</value> </parameter> <parameter> <name>IPAddress</name> <value>192.168.1.1</value> </parameter> <parameter> <name>Setting One</name> <value>200</value> </parameter> <parameter> <name>Setting Three</name> <value>300</value> </parameter> </ConfigData> Would like to add another node parameter/name, parameter/value, but unsure how to, when there are multiple instances with the same tagname for example, would like to add the following if the nodes do not already exist:
      <parameter> <name>UserPreferredLanguage</name> <value>English</value> </parameter> Currently using the following to check and delete/modify existing nodes, was thinking of placing the name and value into a 2d array and then using a true/false in a 3rd column to return if the setting was found and modified or not, so I can decide whether I need to create the nodes or not, but I'm hoping there is a simpler way.\
      Any assistance would be much appreciated.
      PS: Have also written this with the XML Dom Wrapper UDF but still unsure how to proceed with checking nodes and creating them if they're missing, also couldn't find a function to delete a full node + parent using selectNodes, hence the custom __XML_DeleteNode function.
      Thanks
      Local $sXMLDocPath = @ScriptDir & "\XMLDoc.xml" Local $oXMLDoc = ObjCreate("MSXML2.DOMDocument") $oXMLDoc.validateOnParse = False $oXMLDoc.load($sXMLDocPath) Local $oXMLNodes = $oXMLDoc.selectNodes("ConfigData/parameter") If IsObj($oXMLNodes) Then For $oXMLNode In $oXMLNodes If $oXMLNode.childNodes.item(0).tagName = "Name" And $oXMLNode.childNodes.item(0).Text = "Setting One" Then If $oXMLNode.childNodes.item(1).tagName = "Value" Then $oXMLNode.childNodes.item(1).Text = 10 EndIf If $oXMLNode.childNodes.item(0).tagName = "Name" And $oXMLNode.childNodes.item(0).Text = "IPAddress" Then __XML_DeleteNode($oXMLNode) EndIf If $oXMLNode.childNodes.item(0).tagName = "Name" And $oXMLNode.childNodes.item(0).Text = "Setting Two" Then If $oXMLNode.childNodes.item(1).tagName = "Value" Then $oXMLNode.childNodes.item(1).Text = 20 EndIf If $oXMLNode.childNodes.item(0).tagName = "Name" And $oXMLNode.childNodes.item(0).Text = "Setting Three" Then If $oXMLNode.childNodes.item(1).tagName = "Value" Then $oXMLNode.childNodes.item(1).Text = 30 EndIf Next EndIf $oXMLDoc.Save ($sXMLDocPath) Func __XML_DeleteNode($_oNode_Enum) If $_oNode_Enum.hasChildNodes Then For $_oNode_Enum_Child In $_oNode_Enum.childNodes If $_oNode_Enum_Child.nodeType = $XML_NODE_TEXT Then If StringStripWS($_oNode_Enum_Child.text, $STR_STRIPLEADING + $STR_STRIPTRAILING + $STR_STRIPSPACES) = "" Then $_oNode_Enum.removeChild($_oNode_Enum_Child) EndIf EndIf Next EndIf $_oNode_Enum.parentNode.removeChild($_oNode_Enum) EndFunc  
    • PedroWarlock
      By PedroWarlock
      (GOOGLE TRANSLATOR) Sorry: /
      Sorry, there is no new difficulty to organize my reading system, I need an XLM file like that, which is the problem with 30 thousand lines, is this, I need to find the value "Difficulty" and "live" and tell the program what's with the "default =" yes "" not <dipvalue>, can anyone have an example of how I wanted to do this? An example is enough to do this.
      example.au3
      #include <Array.au3> #include "XML.au3" _Example() Func _Example() Local $oXML = _XML_CreateDOMDocument(Default) Local $sXML_Content =@ScriptDir & "\file.xml" _XML_Load($oXML, $sXML_Content) ; ~ _XML_LoadXML($oXML, $sXML) Local $iNodeCount = _XML_GetNodesCount($oXML, "/mame/game") ConsoleWrite("Group(s): $iNodeCount = " & $iNodeCount & "; @error = " & @error & "; @extended = " & @extended & @LF) Local $aNames = _XML_GetValue($oXML, "/mame/game/description") Local $aTaxCountries = _XML_GetValue($oXML, "/mame/game/manufacturer") Local $aData[$iNodeCount + 1][4] = [[$iNodeCount, "", ""]] For $n = 1 To $iNodeCount Local $oNode_Selected_SingleOne = _XML_SelectSingleNode($oXML, '/mame/game/dipswitch[' & $n & ']') Local $sAttribute_Value = _XML_GetNodeAttributeValue($oNode_Selected_SingleOne, 'name') Local $oNode_Selected_SingleOne2 = _XML_SelectSingleNode($oXML, '/mame/game/dipswitch/dipvalue[' & $n & ']') Local $sAttribute_Value2 = _XML_GetNodeAttributeValue($oNode_Selected_SingleOne2, 'name') $aData[$n][0] = $aNames[$n] $aData[$n][1] = $aTaxCountries[$n] $aData[$n][2] = $sAttribute_Value $aData[$n][3] = $sAttribute_Value2 Next _ArrayDisplay($aData, "$aData") EndFunc ;==>_Example  
      file.xml
      <mame build="0.124a BRarcade(Mar 31 2008)" debug="no"> <game name="puckman" sourcefile="pacman.c"> <description>PuckMan (Japan set 1, Probably Bootleg)</description> <dipswitch name="Service Mode"> <dipvalue name="Off" default="yes"/> <dipvalue name="On"/> </dipswitch> <dipswitch name="Cabinet"> <dipvalue name="Upright" default="yes"/> <dipvalue name="Cocktail"/> </dipswitch> <dipswitch name="Coinage"> <dipvalue name="2 Coins/1 Credit"/> <dipvalue name="1 Coin/1 Credit" default="yes"/> <dipvalue name="1 Coin/2 Credits"/> <dipvalue name="Free Play"/> </dipswitch> <dipswitch name="Lives"> <dipvalue name="1"/> <dipvalue name="2"/> <dipvalue name="3" default="yes"/> <dipvalue name="5"/> </dipswitch> <dipswitch name="Bonus Life"> <dipvalue name="10000" default="yes"/> <dipvalue name="15000"/> <dipvalue name="20000"/> <dipvalue name="None"/> </dipswitch> <dipswitch name="Difficulty"> <dipvalue name="Normal" default="yes"/> <dipvalue name="Hard"/> </dipswitch> </game> <game name="puckmana" sourcefile="pacman.c" cloneof="puckman" romof="puckman"> <description>PuckMan (Japan set 2)</description> <dipswitch name="Rack Test (Cheat)"> <dipvalue name="Off" default="yes"/> <dipvalue name="On"/> </dipswitch> <dipswitch name="Service Mode"> <dipvalue name="Off" default="yes"/> <dipvalue name="On"/> </dipswitch> <dipswitch name="Cabinet"> <dipvalue name="Upright" default="yes"/> <dipvalue name="Cocktail"/> </dipswitch> <dipswitch name="Coinage"> <dipvalue name="2 Coins/1 Credit"/> <dipvalue name="1 Coin/1 Credit" default="yes"/> <dipvalue name="1 Coin/2 Credits"/> <dipvalue name="Free Play"/> </dipswitch> <dipswitch name="Lives"> <dipvalue name="1"/> <dipvalue name="2"/> <dipvalue name="3" default="yes"/> <dipvalue name="5"/> </dipswitch> <dipswitch name="Difficulty"> <dipvalue name="Normal" default="yes"/> <dipvalue name="Hard"/> </dipswitch> </game> <game name="puckmanf" sourcefile="pacman.c" cloneof="puckman" romof="puckman"> <dipswitch name="Cabinet"> <dipvalue name="Upright" default="yes"/> <dipvalue name="Cocktail"/> </dipswitch> <dipswitch name="Lives"> <dipvalue name="1"/> <dipvalue name="2"/> <dipvalue name="3" default="yes"/> <dipvalue name="5"/> </dipswitch> <dipswitch name="Difficulty"> <dipvalue name="Normal" default="yes"/> <dipvalue name="Hard"/> </dipswitch> <dipswitch name="Ghost Names"> <dipvalue name="Normal" default="yes"/> <dipvalue name="Alternate"/> </dipswitch> </game> <game name="puckmanh" sourcefile="pacman.c" cloneof="puckman" romof="puckman"> <dipswitch name="Coinage"> <dipvalue name="2 Coins/1 Credit"/> <dipvalue name="1 Coin/1 Credit" default="yes"/> <dipvalue name="1 Coin/2 Credits"/> <dipvalue name="Free Play"/> </dipswitch> <dipswitch name="Lives"> <dipvalue name="1"/> <dipvalue name="2"/> <dipvalue name="3" default="yes"/> <dipvalue name="5"/> </dipswitch> <dipswitch name="Bonus Life"> <dipvalue name="10000" default="yes"/> <dipvalue name="15000"/> <dipvalue name="20000"/> <dipvalue name="None"/> </dipswitch> <dipswitch name="Difficulty"> <dipvalue name="Normal" default="yes"/> <dipvalue name="Hard"/> </dipswitch> </game> <game name="pacman" sourcefile="pacman.c" cloneof="puckman" romof="puckman"> <dipswitch name="Cabinet"> <dipvalue name="Upright" default="yes"/> <dipvalue name="Cocktail"/> </dipswitch> <dipswitch name="Coinage"> <dipvalue name="2 Coins/1 Credit"/> <dipvalue name="1 Coin/1 Credit" default="yes"/> <dipvalue name="1 Coin/2 Credits"/> <dipvalue name="Free Play"/> </dipswitch> <dipswitch name="Lives"> <dipvalue name="1"/> <dipvalue name="2"/> <dipvalue name="3" default="yes"/> <dipvalue name="5"/> </dipswitch> <dipswitch name="Bonus Life"> <dipvalue name="10000" default="yes"/> <dipvalue name="15000"/> <dipvalue name="20000"/> <dipvalue name="None"/> </dipswitch> </game> <game name="pacmanf" sourcefile="pacman.c" cloneof="puckman" romof="puckman"> <dipswitch name="Coinage"> <dipvalue name="2 Coins/1 Credit"/> <dipvalue name="1 Coin/1 Credit" default="yes"/> <dipvalue name="1 Coin/2 Credits"/> <dipvalue name="Free Play"/> </dipswitch> <dipswitch name="Lives"> <dipvalue name="1"/> <dipvalue name="2"/> <dipvalue name="3" default="yes"/> <dipvalue name="5"/> </dipswitch> <dipswitch name="Bonus Life"> <dipvalue name="10000" default="yes"/> <dipvalue name="15000"/> <dipvalue name="20000"/> <dipvalue name="None"/> </dipswitch> <dipswitch name="Difficulty"> <dipvalue name="Normal" default="yes"/> <dipvalue name="Hard"/> </dipswitch> <dipswitch name="Ghost Names"> <dipvalue name="Normal" default="yes"/> <dipvalue name="Alternate"/> </dipswitch> <driver status="good" emulation="good" color="good" sound="good" graphic="good" savestate="supported" palettesize="512"/> </game> </mame>
    • SchneiMi
      By SchneiMi
      Hello,
      following my previous question, I have moved all potentially instable object interactions into Executes. But it Looks like "="-assignments to object Attributes cannot be done with Execute, only method calls. Using an "$obj = 1" construct, it compares (Eval) instead of sets (Execute) the value.
      I have tested multiple different combinations, using Execute and Assign, but it seems not to work with object Attributes. :-(
      Gives following Output:
      The assign Action using apply (a3) Fails, while assigning it directly, without Assign() works fine.
      The execute versions compare and do not assign, in both cases. Though, the "Execute" topic in the help file says it executes, not evaluates.
      I have found a similar, old thread, which explains this behavious but does not give a solution. https://www.autoitscript.com/forum/topic/110228-pass-object-property-as-a-variable/  
      Is there a way to assign to a com object's Attribute? Or is there anything new to this unexpected behaviour of Execute (at least compared to the help file description and Python's exec).
       
      Any help is appreciated, and thank you for all the help so far.
      Regards, Michael
       
    • Simpel
      By Simpel
      Hi. I'm trying to write a xml. Here is my code:
      #include <_XMLDomWrapper.au3> #include <Date.au3> Global $g_sXMLFileName Global $g_sDestPath = @DesktopDir & "\" Global $g_sReturnedBID = "A10829" _makeXML() _AddXML(1, "A10829_Thomas/wav/T001.wav") _AddXML(2, "A10829_Thomas/wav/T002.wav") Exit Func _makeXML() Local $sXMLtime = StringReplace(StringReplace(StringReplace(_NowCalc()," ","_"),":","-"),"/","-") ; in yyyy-mm-dd_hh-mm-ss $g_sXMLFileName = $g_sDestPath & $g_sReturnedBID & "_" & "EB-Ton-Upload" & "_" & $sXMLtime & ".xml" _XMLCreateFile($g_sXMLFileName, "gemagvl", 1,1) _XMLFileOpen($g_sXMLFileName) EndFunc Func _AddXML($iCount, $sDateiname) _XMLCreateRootNodeWAttr("row", "count", $iCount, "") _XMLCreateChildNode("//row", "picklistenname", $g_sReturnedBID & "_EB-Ton-Upload") _XMLCreateChildNode("//row", "picklisteninfo") _XMLCreateChildNode("//row", "bid", $g_sReturnedBID) _XMLCreateChildNode("//row", "audiodateiname", $sDateiname) _XMLCreateChildNode("//row", "titel", StringTrimRight(StringTrimLeft($sDateiname, 7), 4)) _XMLCreateChildNode("//row", "interpret", "EB") _XMLCreateChildNode("//row", "quelle", "Ton") EndFunc It returns:
      <?xml version="1.0" encoding="UTF-8"?><gemagvl> <row count="1"> <picklistenname>A10829_EB-Ton-Upload</picklistenname> <picklisteninfo/> <bid>A10829</bid> <audiodateiname>A10829_Thomas/wav/T001.wav</audiodateiname> <titel>Thomas/wav/T002</titel> <interpret>EB</interpret> <quelle>Ton</quelle> <picklistenname>A10829_EB-Ton-Upload</picklistenname> <picklisteninfo/> <bid>A10829</bid> <audiodateiname>A10829_Thomas/wav/T002.wav</audiodateiname> <titel>Thomas/wav/T003</titel> <interpret>EB</interpret> <quelle>Ton</quelle> </row> <row count="2"> <picklistenname>A10829_EB-Ton-Upload</picklistenname> <picklisteninfo/> <bid>A10829</bid> <audiodateiname>A10829_Thomas/wav/T002.wav</audiodateiname> <titel>Thomas/wav/T003</titel> <interpret>EB</interpret> <quelle>Ton</quelle> </row> </gemagvl> But it should return:
      <?xml version="1.0" encoding="UTF-8"?><gemagvl> <row count="1"> <picklistenname>A10829_EB-Ton-Upload</picklistenname> <picklisteninfo/> <bid>A10829</bid> <audiodateiname>A10829_Thomas/wav/T001.wav</audiodateiname> <titel>Thomas/wav/T002</titel> <interpret>EB</interpret> <quelle>Ton</quelle> </row> <row count="2"> <picklistenname>A10829_EB-Ton-Upload</picklistenname> <picklisteninfo/> <bid>A10829</bid> <audiodateiname>A10829_Thomas/wav/T002.wav</audiodateiname> <titel>Thomas/wav/T003</titel> <interpret>EB</interpret> <quelle>Ton</quelle> </row> </gemagvl> The second inserted nodes are double. How will it be going right?
      Regards, Conrad
    • rootx
      By rootx
      I need help to read in a loop the DVD id child and subchild. Thx
      Example...
      DVD001 - PAL - EN,FR,DE,ES,IT and filter the right title & descri language.  I tried with $oXML.SelectSingleNode but without success
      <?xml version="1.0" encoding="UTF-8"?> <datafile xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="mydvd.xsd"> <dvd name="My dvd title"> <id>DVD001</id> <region>PAL</region> <languages>EN,FR,DE,ES,IT</languages> <locale lang="EN"> <title>title en</title> <descri>descri en</descri> </locale> <locale lang="FR"> <title>title fr</title> <descri>descri fr </descri> </locale> <locale lang="DE"> <title>title de</title> <descri>descri de </descri> </locale> <locale lang="ES"> <title>title es</title> <descri>descri es</descri> </locale> <locale lang="IT"> <title>title it</title> <descri>descri it</descri> </locale> </dvd> <dvd name="My dvd title 2"> <id>DVD002</id> <region>USA</region> <languages>EN</languages> <locale lang="EN"> <title>title en</title> <descri>descri en</descri> </locale> </dvd> </datafile> #include <File.au3> $xml = @ScriptDir&"\test.xml" Local $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.load($xml) $id = $oXML.SelectNodes("//dvd") For $ids In $id ConsoleWrite($ids.text &@CRLF) Next  
×