Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
set objXMLDOMNode =
objDOMDocument.appendChild(objNewChildNode)
Remark
This method's function is to append (add) a new child Node
to the end of a Node.
Before we can append a new Node (element, attribute, etc.), we
need to first create this Node. Then once we have a reference to
this Node, we can append it to the current parentNode by passing
the parameter (objNewChildNode), which is the newly created Node.
This new child will be added to the end of the list of children
on this parentNode.
The child (objXMLDOMNode) will be returned if it is
successfully added. It will be set to nothing if it fails.
Appending to the
root (DOMDocument)
If you recall, we mentioned that the DOMDocument root could
only have the following child Nodes:
¬ element-only one, because the DOMDocument can only have
one root
element Node (createElement)
Of these child Nodes, the only one that you cannot currently
add to the DOMDocument is a DocumentType (DTD).
Example
Before we can append a child Node, we need to fist create a
Node of whichever type you want (attribute, element, etc.) and
set a few properties to it if needed. This example creates a new
DOMDocument, and then the documentElement gets added to it:
Dim objDOMDocument As DOMDocument
Dim objPeople As IXMLDOMNode
Dim objNode As IXMLDOMNode
Set objDOMDocument = new DOMDocument
Set objPeople =
objDOMDocument.createElement("People")
Set objNode = objDOMDocument.appendChild (objPeople)
¬ Add the root Node to the XML document.
¬ Create the element first.
¬ Append this childNode to the end of the DOMDocument.
Consider the following code:
Set objNode = objDOMDocument.appendChild (objPeople)
¬ You only need to say Set objNode = .
If you want to check that this Node was created properly. You
could just say:
objDOMDocument.appendChild objPeople
Appending to the
root element (documentElement)
If you try to add a second element- type Node to the
DOMDocument, you'll get a run- time error-because the
DOMDocument only allows one root element.
How do you go
about adding more data to your DOMDocument?
No matter how many levels deep your tree goes, you need to add
your first branch, which is your documentElement. Thereafter you
need to append (add) the rest of the XML data.
The following steps show the sequence for adding data to your
XML code, starting with adding the root Node.
Step 1:
<PEOPLE>
....
</PEOPLE>
Then you need to add your next level branch, which will be the
your first subelement of the root Node.
Step 2:
<PEOPLE>
<PERSON>
...
</PERSON>
</PEOPLE>
To this subelement we can then add the elements (data).
You now need to append the root element. The following code
shows Step 2 and Step 3 added to the XML code in VB. (See
createElement() to create the element.)
Dim objPerson As IXMLDOMNode
Dim objChild As IXMLDOMNode
Set objPerson =
objDOMDocument.createElement("PERSON")
Set objChild =
objDOMDocument.createElement("NAME")
objChild.Text = "Monty Python"
objPerson.appendChild objChild
objDOMDocument.save
¬ Create a new element DOMDocument.
¬ We have already inserted our documentElement. Now we
need to append a new
Person element to the documentElement.
¬ Create a new childNode for the Person element and give
it a few properties.
¬ Append this new child to the Person element.
To actually add any new change to the DOMDocument, you need to
save the DOMDocument.
Note
The Node may be added to the instantiated DOMDocument, but it
is not added to the actual XML file until you have called the
save() method.
In Line 2 above we could also say:
objPeople.appendChild objPerson
Either method is fine. Both objPeople and
objDOMDocument.documentElement refer to the Document Element.
This manuscript is an abridged version of a chapter from the
Manning
Publications book XMLProgramming with VB and ASP. This chapter
looks at the Microsoft DOM objects. NOTE: Most images have been
removed to increase speed and many of the code comments have also
been removed for presentation. Please purchase the book to enjoy
the full experience of all the chapters with images and code
comments!