AutoitMike Posted May 19, 2022 Share Posted May 19, 2022 I have a need to add a missing node and using AppendChild works except that I would like to keep the structure the same as when the node is not missing. I want to have the new node added right after "CFName1" / before "CEMail" see attached test file This code I am using changes the node "CEMail" to the value "prop" with no attributes or values, ugggh!! Here is the code: Local $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.load("Report.hr5") $oEmail=$oXML.SelectSingleNode("//ClientData/prop[@name='CEmail']") $oOther=$oXML.SelectSingleNode("//ClientData/prop[@name='CFName1']") $Client1Fname=$oOther.text Local $oNode = $oXML.SelectSingleNode("//ClientData") Local $oNew = $oXML.CreateElement("Prop") Local $oName = $oXML.createAttribute("name") $oName.value = "CLName1" Local $oType = $oXML.createAttribute("type") $oType.value = "0" ;$oNode.AppendChild($oNew) ;If I un-comment this and comment out "InsertBefore" it works but appends to the end of //ClientData $oNode.InsertBefore($oNew,$oEmail) With $oNode.lastChild .attributes.setNamedItem($oName) .attributes.setNamedItem($oType) .text = $Client1Fname EndWith Thanks for your help report.hr5 Link to comment Share on other sites More sharing options...
genius257 Posted May 21, 2022 Share Posted May 21, 2022 Hi @AutoitMike. Your code works fine? The only problem i can see, is your "With" code block. You modify the last child in the ClientData node. That is fine when you use AppendChild, but when using InsertBefore, you instead modify your "CEmail" element. I am also not sure what you mean by: On 5/19/2022 at 5:55 AM, AutoitMike said: I would like to keep the structure the same as when the node is not missing because the structure will NOT be the same, when you add an item in between? So i took it as you want it visually to look the same, when looking at the XML structure. Anyway, here are very few changes that should solve it? Local $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.load("Report.hr5") $oEmail=$oXML.SelectSingleNode("//ClientData/prop[@name='CEmail']") $oOther=$oXML.SelectSingleNode("//ClientData/prop[@name='CFName1']") $Client1Fname=$oOther.text Local $oNode = $oXML.SelectSingleNode("//ClientData") Local $oNew = $oXML.CreateElement("prop") Local $oName = $oXML.createAttribute("name") $oName.value = "CLName1" Local $oType = $oXML.createAttribute("type") $oType.value = "0" ;$oNode.AppendChild($oNew) ;If I un-comment this and comment out "InsertBefore" it works but appends to the end of //ClientData $oNode.InsertBefore($oNew,$oEmail) $oNode.InsertBefore($oXML.createTextNode(@CRLF&@TAB&@TAB), $oEmail) With $oNew .attributes.setNamedItem($oName) .attributes.setNamedItem($oType) .text = $Client1Fname EndWith ConsoleWrite($oXML.xml) I've added a ConsoleWrite at the bottom, so you can review the XML result in the console output. My Scripts: AutoIt Package Manager, AutoItObject Pure AutoIt, Corsair CUE SDK Github: AutoIt HTTP Server, AutoIt HTML Parser 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