Share via


Working with Elements

 

Elements within an XML document are represented by IXMLDOMElement objects. Just as elements can contain elements, text, comments, processing instructions, CDATA sections, and entity references within XML documents, these objects can contain IXMLDOMElement, IXMLDOMText, IXMLDOMComment, IXMLDOMProcessingInstruction, IXMLDOMCDATASection, and IXMLDOMEntityReference objects.

Elements do not store attributes directly as children, however. Attributes can be retrieved or modified by name using the getAttribute and setAttribute methods, or manipulated as an IXMLDOMNamedNodeMap through the attributes property.

Creating an element requires that you already have created a DOMDocument object, as the createElement method belongs to DOMDocument. Although you can create and manipulate elements without adding them to the node list of DOMDocument, these elements will disappear as soon as the program completes and will not be persisted in XML documents. Generally, you'll want to assign elements a place in the document tree shortly after creation.

Examples

The following are script examples.

JScript

The following JScript code creates a DOMDocument object, and then uses that DOMDocument object to create an IXMLDOMElement object, which is then appended to the DOMDocument to be the root element of the document.

var xmlDoc = new ActiveXObject("Msxml2.DOMDocument.6.0");  
var rootElement=xmlDoc.createElement("memo");  
xmlDoc.appendChild(rootElement);  
  

VBScript

Set xmlDoc = CreateObject
("Msxml2.DOMDocument.6.0")  
Set rootElement=xmlDoc.createElement
("memo")  
xmlDoc.appendChild
(rootElement)  
  

For more information about properties and methods of IXMLDOMElement, see IXMLDOMElement.