-
January 22nd, 2008, 09:46 AM
#1
Getting back XML DOM Data
I have a XMLHTTPResponse function like this which receives the returning XML and extracts the information for processing:
PHP Code:
function handleServerResponse()
{
// move forward only if the transaction has completed
if (xmlHttp.readyState == 4)
{
// status of 200 indicates the transaction completed successfully
if (xmlHttp.status == 200)
{
// extract the XML retrieved from the server
xmlResponse = xmlHttp.responseXML;
// obtain the document element (the root element) of the XML structure
xmlDocumentElement = xmlResponse.documentElement;
// get the text message, which is in the first child of
// the the document element
var product_node=xmlDocumentElement.firstChild;
//var product_id = product_node.pr_id;
var pr_name= product_node.firstChild.nodeValue;
var pr_details= product_node.firstChild.nextSibling.nodeValue;
// update the client display using the data received from the server
document.getElementById("details_box").innerHTML = pr_name + "<p />" + pr_details;
// restart sequence
//setTimeout('process(produ)', 1000);
}
else
{
alert("There was a problem accessing the server: " + xmlHttp.statusText);
}
}
}
It seems to work fine and I can traverse my tree and print out nodeName(s) but not nodeValue(s). What am I doing wrong? My firebug extension is showing me that my XML returned the information fine.
Here is my XML:
PHP Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<products>
<product pr_id="1">
<name>washing machine</name>
<details>hoover washing machine, 3yr warranty etc</details>
</product>
</products>
Nibinaear
-
January 22nd, 2008, 01:40 PM
#2
Re: Getting back XML DOM Data
You are a node too shallow and also missing the siblings within each childNode. I would suggest using the following for both simplicity, and efficiency.
Code:
xmlDocumentElement = xmlHttp.responseXML.documentElement;
pr_names = xmlDocumentElement.getElementsByTagName('name');
pr_details = xmlDocumentElement.getElementsByTagName('details');
for(i = 0; i < pr_names.length; i++){
document.getElementById("details_box").innerHTML = pr_names[i].firstChild.nodeValue + "<br />" + pr_details[i].firstChild.nodeValue + "<br />";
}
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
January 22nd, 2008, 01:53 PM
#3
Re: Getting back XML DOM Data
Originally Posted by PeejAvery
You are a node too shallow and also missing the siblings within each childNode. I would suggest using the following for both simplicity, and efficiency.
Code:
xmlDocumentElement = xmlHttp.responseXML.documentElement;
pr_names = xmlDocumentElement.getElementsByTagName('name');
pr_details = xmlDocumentElement.getElementsByTagName('details');
for(i = 0; i < pr_names.length; i++){
document.getElementById("details_box").innerHTML = pr_names[i].firstChild.nodeValue + "<br />" + pr_details[i].firstChild.nodeValue + "<br />";
}
Genius, didn't think of that as I didn't assume that the data think the data itself was a node. I just changed it to:
PHP Code:
var pr_name= product_node.firstChild.firstChild.data;
var pr_details= product_node.firstChild.nextSibling.firstChild.data;
Thanks
Nibinaear
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|