欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

jscript与vbscript 操作XML元素属性的代码

程序员文章站 2022-06-28 16:42:05
although attributes belong to a particular element, they are not considered child node...
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 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".
复制代码 代码如下:

var xmldoc = new activexobject("msxml2.domdocument.3.0");
var rootelement=xmldoc.createelement("memo");
rootelement.setattribute("author", "pat coleman");
xmldoc.appendchild(rootelement);


vbscript
复制代码 代码如下:

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".

复制代码 代码如下:

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
复制代码 代码如下:

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)