162 Part I: Core Ideas
You then loop over each item and extract the text contents of each tag to be printed out
in a table:
for (var i=0;i<items.snapshotLength;i++)
{
var title = document.evaluate('title', items.snapshotItem(i), null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent;
var url = document.evaluate('url', items.snapshotItem(i), null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.textContent;
...snip ...
/* print out the complete bookmark */
}
We omit the messy details of the output, but you can see the complete Firefox-specific
version at http://ajaxref.com/ch4/xmlxpathff.html.
Internet Explorer is different but is actually a bit cleaner looking syntax wise, though the
support is not native to the browser. Because of this, you must create an ActiveX-based
DOM parser and load the XHR’s responseText containing the bookmark list directly.
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
/* we also could use our createDocument() wrapper function here but for
clearly acknowledging the ActiveX parsing involved we leave this here.
The online version does however use the wrapper */
xmlDoc.async="false";
xmlDoc.loadXML(xhr.responseText);
With the document parsed, you can call the selectNodes() method and pass it the
XPath expression. With the returned list of <bookmark> tags, you can then loop over the list
and call selectNodes() again and fetch the text values very directly:
FIGURE 4-7 Bookmark list populated by Ajax and XPath