This site has been taken over by the staff of www.ASPDeveloper.Net

Please report errors to suggest@aspdeveloper.net

BizTalk Utilities CV ,   Jobs ,   Code library  
 
 

Microsoft DOM Objects Reference

Download ReferenceDownload this Reference 

Previous PageTable Of ContentsIndexNext Page

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.

Previous PageTable Of ContentsIndexNext Page

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!

 

Recent Jobs

Software Specialist, Linux - Finlan
Linux Core Technical Project Manage
Graphics designer at Tanzania. Expe
Integration Specialist Needed - Wor
Virtualization Server Infrastructur

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






    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