IXMLDOMElement object

The IXMLDOMElement object represents an element in an XML document.

Creating an element

Use the createElement method from the DOMDocument object to create a new element. You must supply the name of the element.

{Create an element named "Seller_Name".}
nameElement = xmlDoc.createElement("Seller_Name");

Creating a new element with the createElement method does not add the element to an XML document. You must add the element to an XML document or another element with the appendChild method, the insertBefore method, or the replaceChild method.

Adding an element

Use the insertBefore method to insert an element before another element or text node. You must specify the element that you are adding as well as the element or text node that you are adding the element in front of.

{Add the nameElement before addressElement.}
xmlDoc.insertBefore(nameElement,addressElement );

You can also use the appendChild method to add an element after all elements and text nodes in a document.

{Add the element to the XML document.}
xmlDoc.appendChild(nameElement);

An element can be added to either a DOMDocument object or another IXMLDOMElement object.


Retrieving an element

The childNodes collection contains all of the nodes in an XML document. You retrieve a node from the childNodes collection with the item method by the index associated with the object. To find an element in the childNodes collection, retrieve each object as an IXMLDOMNode object, and then test the object’s node type with the nodeType property.

{Retrieve the first node in the childNodes collection and find out if it is an IXMLDOMElement object.}
node = xmlDoc.childNodes.item[0];
if(node.nodeType = NODE_ELEMENT) then
	element = node;
end if;

You can also use the selectSingleNode method to retrieve an element. The selectSingleNode method searches an element for a specified pattern and returns an IXMLDOMNode object representing the first instance of the pattern. If no element matches the pattern, null is returned.

You may use either the XSLPattern or XPath selection languages to specify the pattern you want to search for. Refer to the help file for the Microsoft XML Object Library for more information on the XSLPattern and XPath selection languages.


The following example uses the selection language XPath to search for the first Seller_First_Name element. For more information on specifying the selection language, refer to DOMDocument object.

{Specify XPath as the selection language.}
xmlDoc.setProperty("SelectionLanguage", "XPath");
{Retrieve the first instance of a Seller_First_Name element.}
node = element.selectSingleNode("Seller_First_Name");

You can also use the selectNodes method to return an IXMLDOMNodeList object that contains all of the nodes matching a specified pattern. The following example uses the selection language XPath to search for all of the Seller_First_Name elements. For more information on specifying the selection language, refer to DOMDocument object.

{Specify XPath as the selection language.}
xmlDoc.setProperty("SelectionLanguage", "XPath");
{Retrieve all of the instances of a Seller_First_Name element.}
nodeList = element.selectNodes("Seller_First_Name");

Replacing an element

Use the replaceChild method to replace an node in an XML docuement with an element. The node that you are replacing is returned.

{Replace the text node with an element.}
textNode = xmlDoc.replaceChild(element, textNode);

Normalizing an element

Use the normalize method to normalize an IXMLDOMElement object and its descendants. Normalizing an element combines adjacent text nodes into a single text node. When you normalize an element, text nodes can only be separated by markup, such as tags, comments, and processing instructions.

{Normalize an XML document.}
nameElement.normalize();

Representing an XML element as text

Use the xml property to create an XML representation of an element and its descendants.

{Represent the element and its descendants as XML.}
XMLTextField = element.xml;

Deleting an element

Use the removeNode method to remove an element from a document or another element.

{Remove the address element from the element.}
element.removeNode(addressElement);


Documentation Feedback