Although attributes belong to a participant 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 an ixmlnamednodemap.
Examples
JScript
The following JScript example creates a new document containing a <memo> element, and then creates an attribute named author with a value of "Pat Coleman ".CopyCodeThe Code is as follows: var xmldoc = new activexobject ("msxml2.domdocument. 3.0 ");
VaR rootelement = xmldoc. createelement ("memo ");
Rootelement. setattribute ("author", "Pat Coleman ");
Xmldoc. appendchild (rootelement );
VBScriptCopy codeThe Code is as follows: Set xmldoc = Createobject ("msxml2.domdocument. 3.0 ")
Set rootelement = xmldoc. createelement ("memo ")
Rootelement. setattribute ("author", "Pat Coleman ")
Xmldoc. appendchild (rootelement)
If you 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 Need To create an attribute containing an entity reference, you must use this approach .)
Working with attribute 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 The Code is 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 );
VBScriptCopy codeThe Code is 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)