favorite tutorials for working with an XML response from an Ajax call?

J

Jake Barnes

Okay, after I do an Ajax call and my PHP code returns a bunch of XML,
I'm curious how I format the response to look like pretty HTML. Anyone
got favorite tutorials they'd like to point me to?
 
T

Thomas 'PointedEars' Lahn

Jake said:
I'd appreciate the reply, but I'd rather not mess with XSLT. I'd
rather just use DOM friendly methods to add HTML elements to the
page, filled with values that I pull out of the XML.

I don't see why using an XSLT processor to transform XML into HTML would
not be "DOM friendly".
I guess ideally I'd get back some kind of XML object, and then iterate
over it, outputting HTML.

That would be reinventing the wheel.


PointedEars
 
J

Jake Barnes

Okay, after I do an Ajax call and my PHP code returns a bunch of XML,
I'm curious how I format the response to look like pretty HTML. Anyone
got favorite tutorials they'd like to point me to?

Partly answering my own question, I did stumble upon this article:

Ajax and XML: Five Ajax anti-patterns

http://www.ibm.com/developerworks/xml/library/x-ajaxxml3/index.html

It's a great article. For an example of how to get an XML response and
iterate over it, the article includes these two functions:


var req = null;
function processReqChange() {
if (req.readyState == 4 && req.status == 200 && req.responseXML ) {
var dtable = document.getElementById( 'dataBody' );
var nl = req.responseXML.getElementsByTagName( 'movie' );
for( var i = 0; i < nl.length; i++ ) {
var nli = nl.item( i );
var elYear = nli.getElementsByTagName( 'year' );
var year = elYear.item(0).firstChild.nodeValue;
var elTitle = nli.getElementsByTagName( 'title' );
var title = elTitle.item(0).firstChild.nodeValue;

var elTr = dtable.insertRow( -1 );

var elYearTd = elTr.insertCell( -1 );
elYearTd.innerHTML = year;

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('');
}
}

loadXMLDoc( url );
 
J

Jake Barnes

I don't see why using an XSLT processor to transform XML into HTML would
not be "DOM friendly".


That would be reinventing the wheel.

Can you point me to the wheel then? I think that is what I'm looking
for.
 
T

Thomas 'PointedEars' Lahn

Jake said:
Jake said:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Jake Barnes wrote:
Okay, after I do an Ajax call and my PHP code returns a bunch of XML,
I'm curious how I format the response to look like pretty HTML. Anyone
got favorite tutorials they'd like to point me to?
http://developer.mozilla.org/en/docs/XSLT
I'd appreciate the reply, but I'd rather not mess with XSLT. I'd
rather just use DOM friendly methods to add HTML elements to the
page, filled with values that I pull out of the XML.
I don't see why using an XSLT processor to transform XML into HTML would
not be "DOM friendly".
I guess ideally I'd get back some kind of XML object, and then iterate
over it, outputting HTML.
That would be reinventing the wheel.

Can you point me to the wheel then? I think that is what I'm looking
for.

I already did that. The wheel is called XSLT -- Extensible Stylesheet
Language Transformations. It was invented to transform code in one markup
language into another.

Use an XSLT processor, such as the one provided by the Gecko DOM, to
transform your XML data into HTML ("format [it] to look like"). Then add
the document fragment to your HTML document.

http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_interface_to_XSL_Transformations


PointedEars
 
J

Jake Barnes

Jake said:
Jake Barnes wrote:
[...] Thomas 'PointedEars' Lahn [...] wrote:
Jake Barnes wrote:
Okay, after I do an Ajax call and my PHP code returns a bunch of XML,
I'm curious how I format the response to look like pretty HTML. Anyone
got favorite tutorials they'd like to point me to?
http://developer.mozilla.org/en/docs/XSLT
I'd appreciate the reply, but I'd rather not mess with XSLT. I'd
rather just use DOM friendly methods to add HTML elements to the
page, filled with values that I pull out of the XML.
I don't see why using an XSLT processor to transform XML into HTML would
not be "DOM friendly".
I guess ideally I'd get back some kind of XML object, and then iterate
over it, outputting HTML.
That would be reinventing the wheel.
Can you point me to the wheel then? I think that is what I'm looking
for.

I already did that. The wheel is called XSLT -- Extensible Stylesheet
Language Transformations. It was invented to transform code in one markup
language into another.

Use an XSLT processor, such as the one provided by the Gecko DOM, to
transform your XML data into HTML ("format [it] to look like"). Then add
the document fragment to your HTML document.

http://developer.mozilla.org/en/docs/Using_the_Mozilla_JavaScript_int...

Hmmm. Okay, I see. This is a way to avoid hard-coding the HTML in the
Javascript?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top