Explain how you can read an xml document received
as a response from an AJAX request?
The AJAX response: XML
XML documents
The first and most obvious choice for an output format is the XML document. The
original idea behind the XMLHTTP object was the importing of XML documents, so it's
no surprise that most of the attention went to XML and it's still considered the default
output format.
Example
The server returns this XML document:
function setDataXML(req)
{
var books = req.responseXML.getElementsByTagName('book');
for (var i=0;i<books.length;i++)
{
var x = document.createElement('div');
x.className = 'book';
var y = document.createElement('h3');
y.appendChild(document.createTextNode(getNodeValue(books[i],'title')));
x.appendChild(y);
var z = document.createElement('p');
z.className = 'moreInfo';
z.appendChild(document.createTextNode('By ' +
getNodeValue(books[i],'author') + ', ' + getNodeValue(books[i],'publisher')));
x.appendChild(z);
var a = document.createElement('img');
a.src =
books[i].getElementsByTagName('cover')[0].getAttribute('src');
x.appendChild(a);
var b = document.createElement('p');
b.appendChild(document.createTextNode(getNodeValue(books[i],'blurb')));
x.appendChild(b);
document.getElementById('writeroot').appendChild(x);
}
}
function getNodeValue(obj,tag)
{
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}Although the W3C DOM gives us full access to both the XML document from the server
and the HTML document the data should be shown in, it doesn't give us an elegant,
simple way of extracting exactly that data we need: we have to delve into the XML
document time and again.
The AJAX response: XML
XML documents
The first and most obvious choice for an output format is the XML document. The
original idea behind the XMLHTTP object was the importing of XML documents, so it's
no surprise that most of the attention went to XML and it's still considered the default
output format.
Example
The server returns this XML document:
function setDataXML(req)
{
var books = req.responseXML.getElementsByTagName('book');
for (var i=0;i<books.length;i++)
{
var x = document.createElement('div');
x.className = 'book';
var y = document.createElement('h3');
y.appendChild(document.createTextNode(getNodeValue(books[i],'title')));
x.appendChild(y);
var z = document.createElement('p');
z.className = 'moreInfo';
z.appendChild(document.createTextNode('By ' +
getNodeValue(books[i],'author') + ', ' + getNodeValue(books[i],'publisher')));
x.appendChild(z);
var a = document.createElement('img');
a.src =
books[i].getElementsByTagName('cover')[0].getAttribute('src');
x.appendChild(a);
var b = document.createElement('p');
b.appendChild(document.createTextNode(getNodeValue(books[i],'blurb')));
x.appendChild(b);
document.getElementById('writeroot').appendChild(x);
}
}
function getNodeValue(obj,tag)
{
return obj.getElementsByTagName(tag)[0].firstChild.nodeValue;
}Although the W3C DOM gives us full access to both the XML document from the server
and the HTML document the data should be shown in, it doesn't give us an elegant,
simple way of extracting exactly that data we need: we have to delve into the XML
document time and again.
No comments:
Post a Comment