gcue Posted June 19, 2013 Posted June 19, 2013 (edited) hello world. I am trying to append some child nodes? and remove "default". I am getting thrown off by the long names of each node "<setting name = "Clients" serializeAs="xml">" <?xml version="1.0" encoding="utf-8"?> <configuration> <userSettings> <RAAD.My.MySettings> <setting name="Clients" serializeAs="Xml"> <value> <ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <string>default</string> </ArrayOfString> </value> </setting> </RAAD.My.MySettings> </userSettings> </configuration> $cfg_file = "C:\user.config" $oXML = ObjCreate("Microsoft.XMLDOM") If Not IsObj($oXML) Then MsgBox(0, "", "Unable to create COM session to XML.") Exit EndIf $stest = FileRead($cfg_file) $oXML.LoadXML($stest) ConsoleWrite($oXML.xML) Edited June 19, 2013 by gcue
ZombieKillz Posted June 19, 2013 Posted June 19, 2013 (edited) gcue, Here's how to remove 'default'. EDIT: Watch out, because it will overwrite that file! $cfg_file = "C:\user.config" $oXML = ObjCreate("Microsoft.XMLDOM") If Not IsObj($oXML) Then MsgBox(0, "", "Unable to create COM session to XML.") Exit EndIf $stest = FileRead($cfg_file) ; added this section - borrowed from SmOke_N - http://www.autoitscript.com/forum/topic/23830-censur/?p=166395 $new = StringReplace($stest, "<string>default</string>", "<string></string>") $stest = $new FileOpen("C:\user.config", 2) FileWrite("C:\user.config", $stest) ; section end $oXML.LoadXML($stest) ConsoleWrite($oXML.xML) Edited June 19, 2013 by ZombieKillz
jdelaney Posted June 19, 2013 Posted June 19, 2013 (edited) let me know the specifics if you still need help after this example expandcollapse popup$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) $oNewChild1 = $oXml.createElement("NewChild1") $oNewChild1.text = "textNewChild1" $oNewChild1.setAttribute("Att1","TextAtt1") $oNewChild2 = $oXml.createElement("NewChild2") $oNewChild2.text = "textNewChild2" $oNewChild2.setAttribute("Att2","TextAtt2") $oNewChild3 = $oXml.createElement("NewChild3") $oNewChild3.text = "textNewChild3" $oNewChild3.setAttribute("Att3","TextAtt3") $oNewChild4 = $oXml.createElement("NewChild4") $oNewChild4.text = "textNewChild4" $oNewChild4.setAttribute("Att4","TextAtt4") $oNewChild5 = $oXml.createElement("NewChild5") $oNewChild5.text = "textNewChild5" $oNewChild5.setAttribute("Att5","TextAtt5") ; Remove "default" $oNode = $oXML.selectSingleNode( '//value//string' ) $oNode.text = "" ;...or do you want to delete the node??? ; $oNode.parentNode.removeChild($oNode) ; Add the above, new nodes to //setting $oParent = $oXML.selectSingleNode( '//setting' ) $oParent.appendChild($oNewChild1) $oParent.appendChild($oNewChild2) $oParent.appendChild($oNewChild3) $oParent.appendChild($oNewChild4) $oParent.appendChild($oNewChild5) ConsoleWrite($oXML.xML) Edited June 19, 2013 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.
ZombieKillz Posted June 19, 2013 Posted June 19, 2013 I may have totally missed the boat on this one. I was going for straight deleting the string 'default' from the file.
gcue Posted June 19, 2013 Author Posted June 19, 2013 awesome yes i wanted to remove the default node - it adds them under a different place tho.. i need them to be within <arrayofstring...></array...> thanks for your help!
jdelaney Posted June 19, 2013 Posted June 19, 2013 (edited) I have the sample to delete the default node, above...it's commented out, currently To add the child elems to arrayofstring modify: $oParent = $oXML.selectSingleNode( '//setting' ) to $oParent = $oXML.selectSingleNode( '//arrayofstring' ) Edited June 19, 2013 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.
gcue Posted June 19, 2013 Author Posted June 19, 2013 ah ok that works.. BUT in the interest of cutting down the huge XML file.. there are other arrayofstring sections the one im specifically trying to modify is under <setting name="Clients" serializeAs="Xml"> thank youuu!
jdelaney Posted June 19, 2013 Posted June 19, 2013 (edited) for the delete: $oNode = $oXML.selectSingleNode( '//setting[@name="Clients"]//value//string' ) for the adds: $oParent = $oXML.selectSingleNode( '//setting[@name="Clients"]//ArrayOfString' ) note: these are all case sensitive Edited June 19, 2013 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.
gcue Posted June 20, 2013 Author Posted June 20, 2013 works perfectly thank you!! strange that you have to do it in that sequence. still trying to learn xml editing via DOM thanks for the great example and help.
gcue Posted June 20, 2013 Author Posted June 20, 2013 (edited) thanks again for your help i have one last question though. If I want to go backwards - that is, delete the entries I have entered - how can i delete all the children under the selected Node. I saw RemoveAll here but I cant figure out the syntax I need to use. I tried: $oNode = $oXML.selectSingleNode('//setting[@name="Clients"]//value//string') ;$oNode = $oXML.selectSingleNode('//setting[@name="Clients"]//value') $oNode.parentNode.RemoveAll($oNode) ;$oNode.RemoveAll($oNode) ;$oNode.RemoveAll() Thank you! Edited June 20, 2013 by gcue
jdelaney Posted June 20, 2013 Posted June 20, 2013 (edited) Not sure, I can play around for a min. For now: $oParent = $oNode.parentNode $oChildren = $oParent.childNodes For $oChild In $oChildren $oChild.parentNode.removeChild($oChild) Next edit: yeah, not sure. Looks like it can't be done...workaround above...or, you can delete the parent(which deletes all children), and then recreate it, and your new elements. Edited June 20, 2013 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.
gcue Posted June 20, 2013 Author Posted June 20, 2013 that worked! i was trying these also:$oNode = $oXML.selectSingleNode('//setting[@name="Clients"]//ArrayOfString') ;$oNode.RemoveAll() $oNode.RemoveAll($oNode) no luck
gcue Posted June 20, 2013 Author Posted June 20, 2013 only thing i notice with that solution is this happens if there are no child nodes ==> The requested action with this object has failed.: $oParent = $oNode.parentNode $oParent = $oNode.parentNode^ ERROR
Solution jdelaney Posted June 20, 2013 Solution Posted June 20, 2013 (edited) Where you define $oNode, you should do an error check line prior to acting on it: If isObj($oNode) Then Edited June 20, 2013 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.
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