Code for JScript and VBScript to operate XML Element attributes

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.