SitePoint Sponsor

User Tag List

Results 1 to 3 of 3

Hybrid View

  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Wont Output XML Content

    I keep getting this error when i run this script.

    Error reading the response: ReferenceError - Can't find variable: childNodes
    I want to return the value of the node <content> but keep getting the error above but assume i am on the right track...?

    Thanks

    http://www.callsabroad.gb.com/

    Code:
    function handleServerResponse()
    	{
    	
    		
    		var xmlResponse = xmlHttp.responseXML;
    		
    		//catching potential errors with IE and Opera
    		
    		if(!xmlResponse || !xmlResponse.documentElement)
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    		//catching potential errors with Firefox
    		var rootNodeName = xmlResponse.documentElement.nodeName;
    		if(rootNodeName == "parsererror")
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    			
    		//obtain the XML documents element
    		xmlRoot = xmlResponse.documentElement;
    		
    		//testing that we received the XML document we expect
    		
    		if(rootNodeName != "response" || !xmlRoot.firstChild)
    		throw("Invalid XML Structure:\n" + xmlHttp.responseText);
    		
    		
    		
    		responseText = getNodeValue(xmlRoot.lastChild);
    		//display the user message
    		
    			
    			myDiv = document.getElementById("myDivElement");
    			myDiv.innerHTML = responseText;
    	}
    	
    	
    function getNodeValue(node) {
      var nodeValue = [];
      var i = end = 0;
      var child = null;
      
      if (node.childNodes) {
        end = childNodes.length;
        for (i; i < end; i++) {
          child = node.childNodes[i];
          if (child.nodeName === '#text') {
            nodeValue.push(child.nodeValue);
          }
        }
      } else if (node.nodeValue) {
        nodeValue.push(node.nodeValue);
      }
      
      return nodeValue.join('');
    }

  2. #2
    CSS & JS/DOM Adept bronze trophy
    Join Date
    Mar 2005
    Location
    USA
    Posts
    5,482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So you didn't test getNodeValue() independently?
    Code:
    end = childNodes.length;
    should be
    Code:
    end = node.childNodes.length;
    I used Firefox's Firebug extension to debug your code.
    We miss you, Dan Schulz.
    Learn CSS. | X/HTML Validator | CSS validator
    Dynamic Site Solutions
    Code for Firefox, Chrome, Safari, & Opera, then add fixes for IE, not vice versa.

  3. #3
    SitePoint Evangelist
    Join Date
    Oct 2005
    Posts
    405
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks that sorted it.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •