fschreiner Posted June 24, 2010 Posted June 24, 2010 Hi everybody, I just came accross an issue with modifying XML files. As part of my project I have to update/add paramters of XML files, so I decided to use the UDFs out of _MSXML.au3. As long as I modify xml files like: <?xml version="1.0" encoding="UTF-8"?> <Properties> <Prop1>Test1</Prop1> <Prop2>Test2</Prop2> <Prop3>Test3</Prop3> <Properties> everything is fine, there is a unique name for the tags. Now I have to update a XML file like: <?xml version="1.0" encoding="UTF-8"?> <Properties> <Parameter> <Name>Prop1</Name> <Value>Test1</Value> </Parameter> <Parameter> <Name>Prop2</Name> <Value>Test2</Value> </Parameter> <Parameter> <Name>Prop3</Name> <Value>Test3</Value> </Parameter> <Properties> As there is no unique name for the tags it always updates just the first entry. Does anybody came accross this before? Is there a workaround I could go for? I would need soemthing like -> update /Properties/Parameters/Value where /Properties/Parameters/Name = Prop2 or -> nodeexists /Properties/Parameters/Value where /Properties/Parameters/Name = Prop2 Thanks a lot! Fred
PsaltyDS Posted June 24, 2010 Posted June 24, 2010 (edited) First you need valid XML (root closing tag has no slash): <?xml version="1.0" encoding="UTF-8"?> <Properties> <Parameter> <Name>Prop1</Name> <Value>Test1</Value> </Parameter> <Parameter> <Name>Prop2</Name> <Value>Test2</Value> </Parameter> <Parameter> <Name>Prop3</Name> <Value>Test3</Value> </Parameter> </Properties>Address multiple nodes by 1-based index number. For example, "/Properties/Parameter[2]/Name" is the "Name" node under the second instance of "Parameter": #include <_XMLDOMWrapper.au3> Global $sXMLFile, $iCnt, $aData $sXMLFile = @ScriptDir & "\Test2.xml" _XMLFileOpen($sXMLFile) $iCnt = _XMLGetNodeCount("/Properties/Parameter") ConsoleWrite("$iCnt = " & $iCnt & @LF) For $n = 1 to $iCnt $aData = _XMLGetValue("/Properties/Parameter[" & $n & "]/Name") If Not @error Then ConsoleWrite($n & ": Name = " & $aData[1] & @LF) $aData = _XMLGetValue("/Properties/Parameter[" & $n & "]/Value") If Not @error Then ConsoleWrite($n & ": Value = " & $aData[1] & @LF) NextSee XPath tutorial. Edited June 24, 2010 by PsaltyDS Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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