BizTalk Utilities CV ,   Jobs ,   Code library  
 
Home Page
XML DOM
The DOM Method: abort()
The DOM Method: DOM object methods
The Node Type: NODE_TEXT
The Node Type: NODE_PROCESSING_INSTRUCTION
The Node Type: NODE_NOTATION
The Node Type: NODE_ENTITY_REFERENCE
The Node Type: NODE_ENTITY
The Node Type: NODE_ELEMENT
The Node Type: NODE_DOCUMENT_TYPE
The Node Type: NODE_DOCUMENT
The Node Type: NODE_COMMENT
The Node Type: NODE_CDATA_SECTION
The Node Type: NODE_ATTRIBUTE
The Node Type: 12 XML DOM Node types
Want to use a treeview control with the DOM, or want to learn about the DOM
Beginners demo into working with the DOM object
Using the XML DOM and session variables
Programming the SAX2 interfaces from Visual Basic.
QuickParse (updated!)
MSXML Parser Sniffer
<< XHTML
XmlSerializer >>

By :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.
First posted :03/24/2008
Times viewed :3183

 

appendChild()

Is a member of:
DOMDocument
XMLDOMNode
XMLDOMAttribute
XMLDOMCDATASection
XMLDOMComment
XMLDOMDocumentFragment
XMLDOMDocumentType
XMLDOMElement
XMLDOMEntity
XMLDOMEntityReference
XMLDOMNotation
XMLDOMProcessingInstruction
XMLDOMText
XTLRuntime

Syntax

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)

¬ processing instruction (createProcessingInstruction)

¬ comment (createComment)

¬ document type (DocumentType)

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).

Step 3:

<PEOPLE>

<PERSON>

<NAME>Mark Wilson</NAME>

<ADDRESS>911 Somewhere Circle, Canberra, Australia</ADDRESS>

<TEL>(++612) 12345</TEL>

<FAX>(++612) 12345</FAX>

<EMAIL>markwilson@somewhere.com</EMAIL>

</PERSON>

</PEOPLE>

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")

objDOMDocument.documentElement.appendChild objPerson

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 XML Programming 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!


Rate this article on a scale of 1 to 10

Your vote :  


 

Recent Jobs

A great opportunity to Digital Vide
here is a greate opportunity as a S
A great opportunity as a Network En
A Greate Opportunituy as a SQL Deve
An immediate job opportunity as a B

View all Jobs (Add yours)
View all CV (Add yours)



Information Online

swimming pool builder
chicago web site design
spfxmasks
Cheap Web Hosting
conference calling
Gucci sunglasses
answering service


    Email TopXML  

Front Page Daily Stuff TopXML Forum XML blogs XML Newsgroups BizTalk Biztalk Utilities Biztalk Utilities Tutorial B2B SAP XML Microsoft .NET Dotnet System XML Soapformatter SQLXML XMLserializer XQuery PHP PHP SimpleXML PHP XML Dom PHP XML RPC PHP XSLT Java Java Java XML Xalan Microsoft ASP ASP Schemas XML SQL Server XML XMLDom XSL XSL Tutorial XSLT Stylesheets General Javascript CSS XHTML WAP