JimmyCollins Posted November 8, 2007 Share Posted November 8, 2007 Hi, I am fairly new to AutoIt and am basically trying to do this: - Open an xml file - Change some attributes - Close Here is my code: CODE ; Get the xml file $filename = @ScriptDir & "\sample.xml" ; Parse the result $oXML = _CreateMSXMLObj() If Not IsObj($oXML) Then MsgBox(0, "_CreateMSXMLObj()", "ERROR!: Unable to create MSXML Object!!", 10) Exit 1 EndIf $oXML.async = False $error = $oXML.Load($filename) If Not $error Then MsgBox(0,"Load XML", "An error occurred loading " & $filename, 10 ) Exit 1 EndIf $oXMLRoot = $oXML.documentElement $oAppNode = $oXML.selectSingleNode("//customers") For $oXmlNode In $oAppNode.childNodes ;------------------------------------------------------------------------------------------------------------------------------- ; Problem is here $new_name = "smith, tom" $new_age = "43" $oSubdataNode = $oXMLNode.firstchild MsgBox(0x40, "Debug", "Changing Attributes.", 2) $oSubdataNode.setAttribute("name", $new_name) $oSubdataNode.setAttribute("age", $new_age) ;------------------------------------------------------------------------------------------------------------------------------- Next Func _CreateMSXMLObj() ; Creates a MSXML instance depending on the version installed on the system $xmlObj = ObjCreate("Msxml2.DOMdocument.6.0") ; Latest available, default in Vista If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.5.0") ; Office 2003 If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.4.0") If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.3.0") ; XP and w2k3 server If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.2.6") ; Win98 ME... If IsObj($xmlObj) Then Return $xmlObj Return 0 EndFunc And here is a sample XML file in the same format as the one I am working with: CODE <xml> <customers> <jc> <subdata name='jimmy collins' age='21' sex='male' city='cork'> </subdata> </jc> <ts> <subdata name='tom smith' age='21' sex='male' city='cork'> </subdata> </ts> <bg> <subdata name='bill gates' age='47' sex='male' city='DC'> </subdata> </bg> </customers> </xml> Can anybody please help me? This has been driving me nuts for the past two days. Thanks, Jimmy Link to comment Share on other sites More sharing options...
PsaltyDS Posted November 8, 2007 Share Posted November 8, 2007 What is it that goes wrong? This works for me: expandcollapse popup; Get the xml file $filename = @ScriptDir & "\Test_1.xml" ; Parse the result $oXML = _CreateMSXMLObj() If Not IsObj($oXML) Then MsgBox(0, "_CreateMSXMLObj()", "ERROR!: Unable to create MSXML Object!!", 10) Exit 1 EndIf $oXML.async = False $error = $oXML.Load ($filename) If Not $error Then MsgBox(0, "Load XML", "An error occurred loading " & $filename, 10) Exit 1 EndIf $oXMLRoot = $oXML.documentElement $oAppNode = $oXML.selectSingleNode ("//customers") For $oXmlNode In $oAppNode.childNodes $new_name = "smith, tom" $new_age = "43" $oSubdataNode = $oXmlNode.firstchild ConsoleWrite("Debug: Changing Attributes." & @LF) $oSubdataNode.setAttribute ("name", $new_name) $oSubdataNode.setAttribute ("age", $new_age) Next $oXML.Save ($filename) Func _CreateMSXMLObj() ; Creates a MSXML instance depending on the version installed on the system $xmlObj = ObjCreate("Msxml2.DOMdocument.6.0") ; Latest available, default in Vista If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.5.0") ; Office 2003 If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.4.0") If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.3.0") ; XP and w2k3 server If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.2.6") ; Win98 ME... If IsObj($xmlObj) Then Return $xmlObj Return 0 EndFunc ;==>_CreateMSXMLObj I just tweaked the filename to fit my local stuff, changed the MsgBox to console write because MsgBoxes are annoying, and added the $oXML.Save to save the results. What do you get? 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 Link to comment Share on other sites More sharing options...
JimmyCollins Posted November 8, 2007 Author Share Posted November 8, 2007 What is it that goes wrong? This works for me: expandcollapse popup; Get the xml file $filename = @ScriptDir & "\Test_1.xml" ; Parse the result $oXML = _CreateMSXMLObj() If Not IsObj($oXML) Then MsgBox(0, "_CreateMSXMLObj()", "ERROR!: Unable to create MSXML Object!!", 10) Exit 1 EndIf $oXML.async = False $error = $oXML.Load ($filename) If Not $error Then MsgBox(0, "Load XML", "An error occurred loading " & $filename, 10) Exit 1 EndIf $oXMLRoot = $oXML.documentElement $oAppNode = $oXML.selectSingleNode ("//customers") For $oXmlNode In $oAppNode.childNodes $new_name = "smith, tom" $new_age = "43" $oSubdataNode = $oXmlNode.firstchild ConsoleWrite("Debug: Changing Attributes." & @LF) $oSubdataNode.setAttribute ("name", $new_name) $oSubdataNode.setAttribute ("age", $new_age) Next $oXML.Save ($filename) Func _CreateMSXMLObj() ; Creates a MSXML instance depending on the version installed on the system $xmlObj = ObjCreate("Msxml2.DOMdocument.6.0") ; Latest available, default in Vista If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.5.0") ; Office 2003 If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.4.0") If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.3.0") ; XP and w2k3 server If IsObj($xmlObj) Then Return $xmlObj $xmlObj = ObjCreate("Msxml2.DOMdocument.2.6") ; Win98 ME... If IsObj($xmlObj) Then Return $xmlObj Return 0 EndFunc ;==>_CreateMSXMLObj I just tweaked the filename to fit my local stuff, changed the MsgBox to console write because MsgBoxes are annoying, and added the $oXML.Save to save the results. What do you get? Hi, It just does nothing, the program executes fine, but does not change the attributes. Link to comment Share on other sites More sharing options...
PsaltyDS Posted November 8, 2007 Share Posted November 8, 2007 Hi,It just does nothing, the program executes fine, but does not change the attributes.Did you try my version? Because it worked fine for me, and yours did not have the $oXML.Save in it. 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 Link to comment Share on other sites More sharing options...
JimmyCollins Posted November 8, 2007 Author Share Posted November 8, 2007 Did you try my version? Because it worked fine for me, and yours did not have the $oXML.Save in it. Just tried it now. t works when I add this in.Thanks for your help man. Link to comment Share on other sites More sharing options...
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