Jump to content

XML InsertBefore destroys child node


Recommended Posts

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

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.

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...