PDA

View Full Version : Working with Client Javascript XML DOM



bruceallen61
02-02-2010, 06:51 AM
I'm implementing/learning some ajax things, but I'm a bit stuck on how to work with the xml result. I'm basing my code off Dynamic Drives ajax examples.

http://www.dynamicdrive.com/dynamicindex17/ajaxroutine.htm

The text version comes back fine and displays all the relevant echoed text from the server, but when the XMl version echos back I simply get the DOM. The examples on Dynamic drive don't include anything for working
client side with the DOM and I've scoured the net and tried numerous things in an attempt to work directly
with the return xml.


function processGetPost()
{

var myajax=ajaxpack.ajaxobj
var myfiletype=ajaxpack.filetype

if (myajax.readyState == 4)
{ //if request of file completed
document.getElementById("img-l").className='nothere2';
document.getElementById("btncc").className='there';
if (myajax.status==200 || window.location.href.indexOf("http")==-1)
{ //if request was successful or running script locally
if (myfiletype=="txt")
{
alert(myajax.responseText)
}
else
{
alert(myajax.responseXML);
....

Result is alert message box: [object Document]


So the question is how do I work directly with the xml/read it.

Say my xml looked something like this...

<xmlresponse>
<result><? echo $result ?></result>
<code><? echo $code ?></code>
<fname><? echo $fname ?></fname>
<lname><? echo $lname ?></lname>
</xmlresponse>

How do I work with the XML DOM in order to display/get/work with fname?

bruceallen61
02-03-2010, 05:41 PM
I managed to figured this out eventually, but I'm running into a snag with IE. Anyone have any ideas. Below is the working code in FF/chrome, doesn't seem to operate correctly in IE 6 or 8, havent tried other versions of IE yet.

//XML

<xmlresponse>
<transaction>
<result><? echo $result ?></result>
<code><? echo $code ?></code>
<fname><? echo $fname ?></fname>
<lname><? echo $lname ?></lname>
</transaction>
</xmlresponse>

alert(myajax.responseXML);

//The code

var xmlobj=myajax.responseXML

//working in FF & chrome
alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[1].textContent); //result ok
alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[3].textContent); //code ok
alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[5].textContent); //fname ok
alert(xmlobj.getElementsByTagName("transaction")[0].childNodes[7].textContent); //lname ok


When I attempt to get the result in a message box in IE. I get the first message/alert box displaying [object]. Then the following message boxes don't display.

Anyone know why this is?

bruceallen61
02-09-2010, 03:37 PM
anyone have any ideas on this?