Code _xml example of JScript and VBScript manipulating XML element properties

Source: Internet
Author: User
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)

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.