04.11.2015 Views

javascript

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 15: XML in JavaScript<br />

To create a new XML document with document element of < root > , the following code can be used:<br />

var xmldom = document.implementation.createDocument(“”, “root”, null);<br />

alert(xmldom.documentElement.tagName); //”root”<br />

var child = xmldom.createElement(“child”);<br />

xmldom.documentElement.appendChild(child);<br />

This example creates an XML DOM document with no default namespace and no doctype . Note that<br />

even though a namespace and doctype aren ’ t needed, the arguments must still be passed in. An empty<br />

string is passed as the namespace URI so that no namespace is applied, and null is passed as the<br />

doctype. The xmldom variable contains an instance of the DOM Level 2 Document type, complete with<br />

all of the DOM methods and properties discussed in Chapter 11 . In this example, the document<br />

element ’ s tag name is displayed and then a new child element is created and added.<br />

You can check to see if DOM Level 2 XML support is enabled in a browser by using the following line of<br />

code:<br />

var hasXmlDom = document.implementation.hasFeature(“XML”, “2.0”);<br />

In practice, it is rare to create an XML document from scratch and then build it up systematically using<br />

DOM methods. It is much more likely that an XML document needs to be parsed into a DOM structure<br />

or vice versa. Because DOM Level 2 didn ’ t provide for such functionality, a couple of de facto standards<br />

emerged.<br />

The DOM Parser Type<br />

Firefox introduced the DOMParser type specifically for parsing XML into a DOM document, and it was<br />

later adopted by Opera. To use it, you must first create an instance of DOMParser and then call the<br />

parseFromString() method. This method accepts two arguments: the XML string to parse and a<br />

content type, which should always be “ text/xml ” . The return value is an instance of Document .<br />

Consider the following example:<br />

var parser = new DOMParser();<br />

var xmldom = parser.parseFromString(“ < root > < child/ > < /root > ”, “text/xml”);<br />

alert(xmldom.documentElement.tagName); //”root”<br />

alert(xmldom.documentElement.firstChild.tagName); //”child”<br />

var anotherChild = xmldom.createElement(“child”);<br />

xmldom.documentElement.appendChild(anotherChild);<br />

var children = xmldom.getElementsByTagName(“child”);<br />

alert(children.length); //2<br />

In this example, a simple XML string is parsed into a DOM document. The DOM structure has < root ><br />

as the document element with a single < child > element as its child. You can then interact with the<br />

returned document using DOM methods.<br />

516

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

Saved successfully!

Ooh no, something went wrong!