04.11.2015 Views

javascript

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

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

Chapter 15: XML in JavaScript<br />

(continued)<br />

var input = event.input;<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 />

}, false);<br />

//begin parsing<br />

parser.parse(input);<br />

If a parsing error occurs during an asynchronous parse operation, the load event never fires. To catch<br />

those errors, you need to define an error handler using a special interface on the LSParser object called<br />

domConfig .<br />

A change in Opera 9.5 causes the load event to never fire, regardless of the validity<br />

of the XML being parsed. It is unclear if this was intentional or is a bug.<br />

The domConfig property is an instance of the DOMConfiguration type, which was introduced in DOM<br />

Level 3 to indicate the parsing and formatting rules for a specific document. The LSParser also uses this<br />

object to specify additional configuration information. You can set such information using the<br />

setParameter() method. One of the parameters is “ error - handler ” , which is a function that handles<br />

parsing errors. As you can see in the following example, this function receives an exception object as an<br />

argument, allowing you to handle the parsing error appropriately:<br />

var implementation = document.implementation;<br />

var parser = implementation.createLSParser(implementation.MODE_ASYNCHRONOUS, null);<br />

var input = implementation.createLSInput();<br />

input.stringData = “ < root > < child/ > < /root > ”;<br />

//subscribe to load event<br />

parser.addEventListener(“load”, function(event){<br />

var xmldom = event.newDocument;<br />

var input = event.input;<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 />

}, false);<br />

520

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

Saved successfully!

Ooh no, something went wrong!