18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 20<br />

Four methods are used to deal with the names of XML nodes:<br />

❑<br />

❑<br />

❑<br />

❑<br />

name() — Returns the qualified name of the node, which is a QName object<br />

localName() — Returns the local name of the node, which is equivalent to<br />

name().localName<br />

setName(qname) — Sets the qualified name of the node<br />

setLocalName(localname) — Sets the local name of the node<br />

Example:<br />

var oXml = Hello World!<br />

oXml.setLocalName(“msg”);<br />

//changes the code to Hello World!<br />

oXml.setName(new QName(“mess”)); //chnages the code to Hello World!<br />

Here, the XML code is changed twice, changing to and then to .<br />

The last method is text(), which returns the text (simple content) of an element:<br />

var oXml = Nicholas C. Zakas;<br />

var sName = oXml.text(); //returns “Nicholas C. Zakas”;<br />

The XMLList Class<br />

614<br />

The XMLList class, briefly introduced in the previous section, represents an array of XML objects. Just as<br />

with the XML object, you have a number of ways to create an XMLList object.<br />

First, you can use the constructor and pass in an XML string containing a number of elements that aren’t<br />

enclosed by a root element. <strong>For</strong> example:<br />

var oXmlList = new XMLList(“Nicholas C. ZakasMichael<br />

Smith”);<br />

In this example, a string of two elements is passed into the XMLList, which creates two separate<br />

XML objects and stores them. Alternately, you can use the plus sign with existing XML objects to<br />

create an XMLList:<br />

var oXml1 = Nicholas C. Zakas;<br />

var oXml2 = Michael Smith;<br />

var oXmlList = oXml1 + oXml2;<br />

These three lines perform the exact same function as the single line shown previously, creating two XML<br />

objects and storing them in a new XMLList object. But you also have one more way to create an XMLList:<br />

var oXmlList = Nicholas C. ZakasMichael Smith;<br />

This form is the XML literal for an XMLList object. The important syntax is the empty opening and closing<br />

tags, indicating this isn’t a typical XML object (empty tags are illegal in XML).<br />

As mentioned in the previous section, the XML and XMLList objects are purposely similar to blur the distinction<br />

between the two. As such, XMLList objects have all the same methods as XML objects, although<br />

they behave a bit differently.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!