Although attributes belong to a particular element, they are not considered child nodes of element nodes. Instead, they behave more like properties of IXMLDOMElement.
Most of the methods for working with attributes come from ixmldomelement. Attributes can be manipulated in the following ways.
Directly, through the GetAttribute and setattribute methods of IXMLDOMElement.
As named Ixmldomattribute nodes, with GetAttributeNode and Setattributenode.
As a set of nodes accessible through the Attributes property and returned as a ixmlnamednodemap.
Examples
Jscript
The following JScript example creates a new document containing a <memo> element, and then creates an attribute name D Author with a value of "Pat Coleman".
Copy Code code as follows:
var xmldoc = new ActiveXObject ("msxml2.domdocument.3.0");
var rootelement=xmldoc.createelement ("Memo");
Rootelement.setattribute ("Author", "Pat Coleman");
Xmldoc.appendchild (rootelement);
VBScript
Copy Code code as follows:
Set xmldoc = CreateObject ("msxml2.domdocument.3.0")
Set rootelement=xmldoc.createelement ("Memo")
Rootelement.setattribute ("Author", "Pat Coleman")
Xmldoc.appendchild (rootelement)
If you are prefer to work with attribute nodes, you can create the attribute, and then create a-text node to store its value. Attribute nodes can only contain text nodes and entity reference nodes. (If you are need to create an attribute containing a entity reference, you are must use this approach.)
Working with attributes nodes requires using the DOMDocument object to create attribute and text (and entity reference, if necessary) nodes before assigning the nodes to the element.
Jscript
The following JScript code uses this approach to perform the same work as the preceding examples, creating a <memo> element with an author attribute holding the value "Pat Coleman".
Copy Code code as follows:
var xmldoc = new ActiveXObject ("msxml2.domdocument.3.0");
var rootelement=xmldoc.createelement ("Memo");
var Memoattribute=xmldoc.createattribute ("author");
var memoattributetext=xmldoc.createtextnode ("Pat Coleman");
Memoattribute.appendchild (Memoattributetext);
Rootelement.setattributenode (Memoattribute);
Xmldoc.appendchild (rootelement);
VBScript
Copy Code code as follows:
Set xmldoc = CreateObject ("msxml2.domdocument.3.0")
Set rootelement= Xmldoc.createelement ("Memo")
Set Memoattribute=xmldoc.createattribute ("author")
Set memoattributetext= Xmldoc.createtextnode ("Pat Coleman")
Memoattribute.appendchild (memoattributetext)
Rootelement.setattributenode (Memoattribute)
Xmldoc.appendchild (rootelement)