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 />

namespace — if namespaces aren ’ t used, this should be set to null . The fourth argument, the type of<br />

result to return, is one of the following 10 constants values:<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

❑<br />

XPathResult.ANY_TYPE — Returns the type of data appropriate for the XPath expression.<br />

XPathResult.NUMBER_TYPE — Returns a number value.<br />

XPathResult.STRING_TYPE — Returns a string value.<br />

XPathResult.BOOLEAN_TYPE — Returns a Boolean value.<br />

XPathResult.UNORDERED_NODE_ITERATOR_TYPE — Returns a node set of matching nodes,<br />

although the order may not match the order of the nodes within the document.<br />

XPathResult.ORDERED_NODE_ITERATOR_TYPE — Returns a node set of matching nodes in the<br />

order in which they appear in the document. This is the most commonly used result type.<br />

XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE — Returns a node set snapshot, capturing<br />

the nodes outside of the document so that any further document modification doesn ’ t affect the<br />

node set. The nodes in the node set are not necessarily in the same order as they appear in the<br />

document.<br />

XPathResult.ORDERED_NODE_SNAPSHOT_TYPE — Returns a node set snapshot, capturing the<br />

nodes outside of the document so that any further document modification doesn ’ t affect the<br />

result set. The nodes in the result set are in the same order as they appear in the document.<br />

XPathResult.ANY_UNORDERED_NODE_TYPE — Returns a node set of matching nodes, although<br />

the order may not match the order of the nodes within the document.<br />

XPathResult.FIRST_ORDERED_NODE_TYPE — Returns a node set with only one node, which is<br />

the first matching node in the document.<br />

The type of result you specify determines how to retrieve the value of the result. Here ’ s a typical<br />

example:<br />

var result = xmldom.evaluate(“employee/name”, xmldom.documentElement, null,<br />

XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);<br />

if (result !== null) {<br />

var node = result.iterateNext();<br />

while(node) {<br />

alert(node.tagName);<br />

node = node.iterateNext();<br />

}<br />

}<br />

This example uses the XPathResult.ORDERED_NODE_ITERATOR_TYPE result, which is the most<br />

commonly used result type. If no nodes match the XPath expression, evaluate() returns null ;<br />

otherwise, it returns an XPathResult object. The XPathResult has properties and methods for<br />

retrieving results of specific types. If the result is a node iterator, whether it be ordered or unordered, the<br />

iterateNext() method must be used to retrieve each matching node in the result. When there are no<br />

further matching nodes, iterateNext() returns null .<br />

531

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

Saved successfully!

Ooh no, something went wrong!