AutoitMike Posted May 20, 2022 Posted May 20, 2022 (edited) Someone please help. I am trying to insert a new node before a certain node, but I get an error, Variable must be of type "Object".: Code is: Local $oXML = ObjCreate("Microsoft.XMLDOM") Local $oResult $oXML.load("C:\Utilities\Temp\report.hr5") Local $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) ;Append works if I un comment this line and comment out the following line $oResult.InsertBefore($oNew,$oEmail) ;This is where it fails With $oNode.lastChild .attributes.setNamedItem($oName) .attributes.setNamedItem($oType) .text = $Client1Fname EndWith $oxml.save("C:\Utilities\Temp\report.hr5") Thanks for your assistance report.hr5 Edited May 20, 2022 by AutoitMike
AutoitMike Posted May 20, 2022 Author Posted May 20, 2022 OK, I figured it out. ;If using "Append" use the following: With $oNode.lastChild .attributes.setNamedItem($oName) .attributes.setNamedItem($oType) .text = $Client1Fname EndWith ;If using "Insert" use the following: With $oNew .attributes.setNamedItem($oName) .attributes.setNamedItem($oType) .text = $Client1Fname EndWith
Subz Posted May 20, 2022 Posted May 20, 2022 Alternative Local $oXML = ObjCreate("Microsoft.XMLDOM") $oXML.load("C:\Utilities\Temp\report.hr5") Local $oEmail=$oXML.SelectSingleNode("//ClientData/prop[@name='CEmail']") Local $oOther=$oXML.SelectSingleNode("//ClientData/prop[@name='CFName1']") $Client1Fname=$oOther.text Local $oNode = $oXML.SelectSingleNode("//ClientData") Local $oNew = $oXML.CreateElement("Prop") $oNew.setAttribute("name", "CLName1") $oNew.setAttribute("type", "0") $oNew.text = $Client1Fname ;~ Insert new node $oNode.InsertBefore($oNew,$oEmail) ;~ Append new node ;~ $oNode.AppendChild($oNew) $oxml.save("C:\Utilities\Temp\report.hr5")
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