PDA

View Full Version : how to handle ajax.responseXML?



xnpeng
03-23-2006, 03:44 AM
http://www.dynamicdrive.com/dynamici...jaxroutine.htm

What's the difference between responseText and responseXML?

when I get response from a jsp file, alert(ajax.responseText) ,it shows:



<?xml version="1.0" encoding="utf-8"?>
<employees>
<employee name="J.Doe">
<job>Programmer</job>
<salary>32768</salary>
</employee>
<employee name="A.Baker">
<job>Sales</job>
<salary>70000</salary>
</employee>
<employee name="Big Cheese">
<job>CEO</job>
<salary>100000</salary>
</employee>
</employees>


while use alert(ajax.responseXML), it shows [object].

the point is that I need retrieve certain data in this XML, that is how to handle xmlObj = ajax.responseXML?
xmlObj.getELementsByTagName("employee")[0].firstChild.data does not work.

I found Microsoft.XMLDOM object can load (not open) an xml file into an xmlObj,which can be handle like xmlObj.getELementsByTagName("employee")[0].firstChild.data .

So what is the difference between XMLDOM and XMLHTTP?

ddadmin
03-23-2006, 05:41 PM
Yep, you'll want to use ajax.responseXML then. It returns a XML object that can be parsed using standard DOM methods. In the case of:


<?xml version="1.0" encoding="utf-8"?>
<employees>
<employee name="J.Doe">
<job>Programmer</job>
<salary>32768</salary>
</employee>
<employee name="A.Baker">
<job>Sales</job>
<salary>70000</salary>
</employee>
<employee name="Big Cheese">
<job>CEO</job>
<salary>100000</salary>
</employee>
</employees>

You can get to the first employee's salary by doing something like:


var xmlobj=ajax.responseXML
var DoeSalary=xmlobj.getElementsByTagName("employee")[0].childNodes[1].nodeValue

A handy reference with the various DOM element methods can be found here (http://www.javascriptkit.com/domref/elementproperties.shtml).