↑
Main Page
XMLList
If you want to create a complete (deep) copy of some XML, you can use the
copy()
method. This
method returns an exact copy of the node and all its descendants (no ancestors are copied).
var oXml = <message>Hello World!</message>
var oNewXml = oXml.copy();
alert(oNewXml.toXMLString()); //outputs <message>Hello World!</message>
The XML object has a method called
length()
which always returns
1
. This may seem a silly, but it
was done to blur the distinction between
XML
objects and
XMLList
objects when writing code. Another
method,
contains()
, is equally useless in the context of XML objects, but it is included to make the
code compatible with
XMLList
objects as well. The
contains()
method returns
true
only when you
pass in the XML object calling the method, like this:
oXml.contains(oXml);
Again, these two methods have actual uses in
XMLList
, but aren’t terribly useful in
XML
.
The
nodeKind()
method determines what type of node an XML object represents, returning either “text”,
“element”, “comment”, “processing-instruction”, or “attribute”. Consider the following XML object:
var oXml = <employees>
<? Don’t forget the donuts! ?>
<employee position=”President”>
<name>Richard McMichael</name>
</employee>
<!-- just added -->
<employee position=”Vice President”>
<name>Rebecca Smith</name>
</employee>
</employees>
Given this XML, the following table shows what
nodeKind()
returns depending on which node is
in scope.
Statement
Returns
oXml.nodeKind()
“element”
oXml.child(0)
“processing-instruction”
oXml.employee.@position.nodeKind()
“attribute”
oXml.employee.nodeKind()
“element”
oXml.child(2)
“comment”
oXml.employee.name.child(0)
“text”
The
normalize()
method works the same way as in the DOM: It normalizes (combines) whitespace
and text between elements to create single text nodes instead of multiple ones. Not very exciting, but
necessary for dealing with XML code.
613
The Evolution of JavaScript
23_579088 ch20.qxd 3/28/05 11:44 AM Page 613
Free JavaScript Editor
Ajax Editor
©
→