XML
The XHR object was made for returning XML results. It has a responseXML property in which
XML is automatically parsed into a navigable object. You can then use familiar DOM methods
to navigate it:
var doc = transport.responseXML.documentElement; // grabs the root node
var songs = doc.getElementsByTagName('song'); // get all song nodes
for(var i=0;i
{
// assuming each node just has text contained within it, we grab the
// text node and display its contents.
alert('I love ' + songs[i].firstChild.data);
}
Using the DOM in XML is a little different from using the DOM in HTML because you lose
some of the conveniences, forcing you to get at data in a slightly different way. The previous
example demonstrates this: in XML, text content is a node unto itself, just as it is in HTML,
and must be retrieved using firstChild (because the text is the first and only child of the element).
Then you use the data property of the text node to retrieve that text. You can also use
the nodeValue property, which behaves the same way.
Let??™s take a look at some example XML and how you can use DOM methods to traverse
that data and insert it into the page. In this example, you convert an XML document into a
number of HTML elements:
CHAPTER 5 n AJAX AND DATA EXCHANGE 106
The Long Road
Hayden Smith
Smith details his battles from the mailroom .
Pages:
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171