Although attnbutes bel ong to a particular eleme nt they are not con sidered child no des of eleme nt
no des. In stead, they behave more like properties of IXMLDOMEIeme nt.
Most of the methods for work ing with attributes come from IXMLDOMEIeme nt. Attributes can be
mam pulated in the followi ng ways.
Directly, through the getAttribute and setAttnbute methods of IXMLDOMEIeme
nt ・. -
As named IXMLDOMAttnbute nodes, with getAttributeNode and setAttributeNode.
As a set of no des accessible through the attributes property and retur ned as an
IXMLNamedNodeM ap.
Examples
JScript
The follow ing JScript example creates a new docume nt containing a o& gt eleme nt, and the n
creates an attribute n amed author wrth a value of *Pat Colema
nL
代码如下:
var xmlDoc = new AdJveXObjed(
-
Msxml2.DOM Docume nL3.(T);
war rootEleme nt=xmlDoc.createEleme nt(
H
memo*);
rootEleme nt.setAttribute(
w
author"
r
"Pat Colema rf);
xmlDoc.appe ndChild(rootEleme nt);
VBScript
代码如下:
Set xmlDoc = CreateObject(
-
Msxml2 DOMDocume nt3.0*)
Set rootEleme nt=xmlDoc.createEleme ntCmemo*)
rootEleme nt. setAttribute("author
11
.
w
Pat Colema n")
xmlDoc.appe ndChild(rootEleme nt)
If you prefer to work with attribute no des. you can create the attribute, and the n create a text node
to store its value. Attribute no des can only contain text no des and en tity reference no des. (If you n eed
to create an attribute containing an entity reference, you must use this approacti.)
Worki ng with attribute no des requires using the DOMDocume nt object to create attribute and text
(a nd en tity referen ce. if n ecessary) no des before assig ning the no des to the eleme nt.
JScript
The followi ng JScript code uses this approach to perform the same work as the precedi ng
examples, creating a <memo> element with an author attribute holding the value "Pat Colema n".
代码如下:
var xmlDoc = new ActiveXObjectCMsxml2. DOM Docume
nt3.(T);
var rootEleme nt=xml Doc. create Eleme nt(
w
memo°);
var memoAttributesxmlDoc.createAttributef^authoT);
var memoAttributeText=xmlDoc createTextNodefPat Colema
n
-
);
评论0
最新资源