↑
Main Page
XMLList Class
Four methods are used to deal with the names of XML nodes:
?
name()
— Returns the qualified name of the node, which is a
QName
object
?
localName()
— Returns the local name of the node, which is equivalent to
name().localName
?
setName(
qname
)
— Sets the qualified name of the node
?
setLocalName(
localname
)
— Sets the local name of the node
Example:
var oXml = <message>Hello World!</message>
oXml.setLocalName(“msg”); //changes the code to <msg>Hello World!</msg>
oXml.setName(new QName(“mess”)); //chnages the code to <mess>Hello World!</mess>
Here, the XML code is changed twice, changing
<message/>
to
<msg/>
and then
<msg/>
to
<mess/>
.
The last method is
text()
, which returns the text (simple content) of an element:
var oXml = <name>Nicholas C. Zakas</name>;
var sName = oXml.text(); //returns “Nicholas C. Zakas”;
The XMLList Class
The
XMLList
class, briefly introduced in the previous section, represents an array of
XML
objects. Just as
with the XML object, you have a number of ways to create an
XMLList
object.
First, you can use the constructor and pass in an XML string containing a number of elements that aren’t
enclosed by a root element. For example:
var oXmlList = new XMLList(“<name>Nicholas C. Zakas</name><name>Michael
Smith</name>”);
In this example, a string of two
<name/>
elements is passed into the
XMLList
, which creates two sepa-
rate
XML
objects and stores them. Alternately, you can use the plus sign with existing
XML
objects to
create an
XMLList
:
var oXml1 = <name>Nicholas C. Zakas</name>;
var oXml2 = <name>Michael Smith</name>;
var oXmlList = oXml1 + oXml2;
These three lines perform the exact same function as the single line shown previously, creating two
XML
objects and storing them in a new
XMLList
object. But you also have one more way to create an
XMLList
:
var oXmlList = <><name>Nicholas C. Zakas</name><name>Michael Smith</name></>;
This form is the XML literal for an
XMLList
object. The important syntax is the empty opening and clos-
ing tags, indicating this isn’t a typical
XML
object (empty tags are illegal in XML).
As mentioned in the previous section, the
XML
and
XMLList
objects are purposely similar to blur the dis-
tinction between the two. As such,
XMLList
objects have all the same methods as
XML
objects, although
they behave a bit differently.
614
Chapter 20
23_579088 ch20.qxd 3/28/05 11:44 AM Page 614
Free JavaScript Editor
Ajax Editor
©
→