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.

Other Node Types<br />

Chapter 16: ECMA Script for XML<br />

E4X is capable of representing all parts of an XML document, including comments and processing<br />

instructions. By default, E4X will not parse comments or processing instructions, so they won ’ t show up<br />

in the object hierarchy. To force the parser to recognize them, the following two properties on the XML<br />

constructor must be set:<br />

XML.ignoreComments = false;<br />

XML.ignoreProcessingInstructions = false;<br />

With these flags set, E4X parses comments and processing instructions into the XML structure.<br />

Since the XML type represents all of the node types, it ’ s necessary to have a way to tell them apart. The<br />

nodeKind() method indicates what type of node an XML object represents, and returns “ text ” ,<br />

“ element ” , “ comment ” , “ processing - instruction ” , or “ attribute ” . Consider the following XML<br />

object:<br />

var employees = < employees ><br />

< ?Dont forget the donuts? ><br />

< employee position=”Software Engineer” ><br />

< name > Nicholas C. Zakas < /name ><br />

< /employee ><br />

< !-- just added -- ><br />

< employee position=”Salesperson” ><br />

< name > Jim Smith < /name ><br />

< /employee ><br />

< /employees > ;<br />

Given this XML, the following table shows what nodeKind() returns, depending on which node is in scope.<br />

Statement<br />

Returns<br />

employees.nodeKind() “ element ”<br />

employees.*[0].nodeKind() “ processing - instruction ”<br />

employees.employee[0].@position.nodeKind() “ attribute ”<br />

employees.employee[0].nodeKind() “ element ”<br />

employees.*[2].nodeKind() “ comment ”<br />

employees.employee[0].name.*[0].nodeKind() “ text ”<br />

The nodeKind() method can ’ t be called on an XMLList that has more than one XML object in it; doing so<br />

throws an error.<br />

555

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

Saved successfully!

Ooh no, something went wrong!