Results 1 to 2 of 2

Thread: [2005] XML > AppendChild

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Sep 2005
    Location
    Alaska
    Posts
    435

    Question [2005] XML > AppendChild

    I have a single XML file that will most likely remain small for the most part.

    Heres the structure:

    <?xml version="1.0" encoding="utf-8"?>
    <Defigo>
    <Server host="domain.com" type="Web" />
    <Server host="127.0.0.1" type="Database" />
    </Defigo>

    I have a form that has 2 textboxes. serverHost and serverType. I'm trying to add a new entry like this:
    <Server host="domain.com" type="Web" />

    when I click buttonCreate.

    I'm able to read the nodes just fine, but writing is a whole new story.

  2. #2
    New Member
    Join Date
    Mar 2011
    Posts
    2

    Re: [2005] XML > AppendChild

    I am trying to understand the function of the XMLDOM command appendChild.

    Here is some code that loads in a simple XML file, that creates a copy of the XML object loaded in, and adds a new 'name' node and then a subsequnt text node of value 'John'.
    This is all well and good, but it also appears that the orginial XML object is also modified.
    It appears that the offending line is this line;
    objCurrNode.appendChild(objNewNode)

    What confuses me, is I have created a copy of the original object, performed some functions on various instances of the copy, appended the result back to the copy, the copy changes (as you would expect) but so too does the orignial object (which I didn't expect)

    here is the full vbs

    Code:
    Dim objXMLDoc
    Dim objCurrNode
    Dim objNewNode
    Dim objNewText
    
    Set objXMLDoc = CreateObject("Microsoft.XMLDOM")
    objXMLDoc.async = False
    objXMLDoc.load("names.xml")
    
    MsgBox "value of objXMLDoc in memory=" & vbCrLf & objXMLDoc.xml
    
    Set objNewNode = objXMLDoc.createElement("name") ' within the XML document object (objXMLDoc), create a new element called objNewNode and assign it the value 'name'
    
    Set objNewText = objXMLDoc.createTextNode("John") ' within XML document object (objXMLDoc), create a text node called objNewText and assign it the value 'John'
    objNewNode.appendChild(objNewText) ' within the node 'objNewNode' (that is called 'name'), append to it a child by passing it the node 'objNewText' (that has a value of 'John')
    
    Set objCurrNode = objXMLDoc.documentElement ' assign to the variable objCurNode, the whole of the XML document object called objXMLDoc
    
    MsgBox "value of objCurrNode in memory1=" & vbCrLf & objCurrNode.xml
    
    objCurrNode.appendChild(objNewNode)
    MsgBox "value of objCurrNode in memory=" & vbCrLf & objCurrNode.xml
    MsgBox "value of objXMLDoc in memory2=" & vbCrLf & objXMLDoc.xml

    and here is the copy of the original xml file called names.xml;
    Code:
    <names>
       <name>Alice</name>
       <name>Bert</name>
       <name>Charlie</name>
       <name>Diane</name>
       <name>Eric</name>
    </names>
    Please could someone explain why it appears that appendChild is changing the original XML object variable (objXMLDoc)?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width