Getting back XML DOM Data
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Getting back XML DOM Data

Hybrid View

  1. #1
    Join Date
    Dec 2004
    Posts
    438

    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_nameproduct_node.firstChild.nodeValue;
                
                var 
    pr_detailsproduct_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

  2. #2
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,865

    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.

  3. #3
    Join Date
    Dec 2004
    Posts
    438

    Re: Getting back XML DOM Data

    Quote 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_nameproduct_node.firstChild.firstChild.data;
                    
                    var 
    pr_detailsproduct_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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center