JavaScript and XML (Page 1 of 4 )
If you want to learn how to use JavaScript to work with XML data, you've come to the right place. This three-part article series starts by showing you how to obtain XML documents. It is excerpted from chapter 21 of
JavaScript: The Definitive Guide, Fifth Edition, written by David Flanagan (O'Reilly; ISBN: 0596101996). Copyright © 2006 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.
The most important feature of the Ajax web application architecture is its ability to script HTTP with the XMLHttpRequest object, which was covered in Chapter 20. The X in “Ajax” stands for XML, however, and for many web applications, Ajax’s use of XML-formatted data is its second most important feature.
This chapter explains how to use JavaScript to work with XML data. It starts by demonstrating techniques for obtaining XML data: loading it from the network, parsing it from a string, and obtaining it from XML data islands within an HTML document. After this discussion of obtaining XML data, the chapter explains basic techniques for working with this data. It covers use of the W3C DOM API, transforming XML data with XSL stylesheets, querying XML data with XPath expressions, and serializing XML data back to string form.
This coverage of basic XML techniques is followed by two sections that demonstrate applications of those techniques. First, you’ll see how it is possible to define HTML templates and automatically expand them, using the DOM and XPath, with data from an XML document. Second, you’ll see how to write a web services client in JavaScript using the XML techniques from this chapter.
Finally, the chapter concludes with a brief introduction to E4X, which is a powerful extension to the core JavaScript language for working with XML.
21.1 Obtaining XML Documents Chapter 20 showed how to use the XMLHttpRequest object to obtain an XML document from a web server. When the request is complete, the responseXML property of the XMLHttpRequest object refers to a Document object that is the parsed representation of the XML document. This is not the only way to obtain an XML Document object, however. The subsections that follow show how you can create an empty XML document, load an XML document from a URL without using XMLHttpRequest, parse an XML document from a string, and obtain an XML document from an XML data island.
As with many advanced client-side JavaScript features, the techniques for obtaining XML data are usually browser-specific. The following subsections define utility functions that work in both Internet Explorer (IE) and Firefox.
Next: 21.1.1 Creating a New Document >>
More JavaScript Articles
More By O'Reilly Media
|
This article is excerpted from chapter 21 of the book JavaScript: The Definitive Guide, Fifth Edition, written by David Flanagan (O'Reilly; ISBN: 0596101996). Check it out today at your favorite bookstore. Buy this book now.
|
|