<script> var req = null; function processReqChange() { if (req.readyState == 4 && req.status == 200 && req.responseXML ) { var dtable = document.getElementById( 'dataBody' ); var nl = req.responseXML.getElementsByTagName( 'book' ); for( var i = 0; i < nl.length; i++ ) { var nli = nl.item( i ); var elAuthor = nli.getElementsByTagName( 'author' ); var author = elAuthor.item(0).firstChild.nodeValue; var elTitle = nli.getElementsByTagName( 'title' ); var title = elTitle.item(0).firstChild.nodeValue; var elTr = dtable.insertRow( -1 ); var elAuthorTd = elTr.insertCell( -1 ); elAuthorTd.innerHTML = author; var elTitleTd = elTr.insertCell( -1 ); elTitleTd.innerHTML = title; } } } function loadXMLDoc( url ) { if(window.XMLHttpRequest) { try { req = new XMLHttpRequest(); } catch(e) { req = false; } } else if(window.ActiveXObject) { try { req = new ActiveXObject('Msxml2.XMLHTTP'); } catch(e) { try { req = new ActiveXObject('Microsoft.XMLHTTP'); } catch(e) { req = false; } } } if(req) { req.onreadystatechange = processReqChange; req.open('GET', url, true); req.send(''); } } var url = window.location.toString(); url = url.replace( /pat2_xml.html/, 'pat2_xml_data.xml' ); loadXMLDoc( url ); </script>
XML:
<books> <book> <author>Jack Herrington</author> <title>Code Generation in Action</title> </book> <book> <author>Jack Herrington</author> <title>Podcasting Hacks</title> </book> <book> <author>Jack Herrington</author> <title>PHP Hacks</title> </book> </books>
json:JavaScript Object Notation(JSON)
var req = null; function processReqChange() { if (req.readyState == 4 && req.status == 200 ) { var dtable = document.getElementById( 'dataBody' ); var books = eval( req.responseText ); for( var b in books ) { var elTr = dtable.insertRow( -1 ); var elAuthorTd = elTr.insertCell( -1 ); elAuthorTd.innerHTML = books[b].author; var elTitleTd = elTr.insertCell( -1 ); elTitleTd.innerHTML = books[b].title; } } } [ { author: 'Jack Herrington', title: 'Code Generation in Action' }, { author: 'Jack Herrington', title: 'Podcasting Hacks' }, { author: 'Jack Herrington', title: 'PHP Hacks' } ]