Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb import XML Elements error - Ajax code

    I do not know the reason.

    In data.xml file, when i do not put html tag like
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <data>
    <pets>
    <pet>Cat</pet>
    <pet>Dog</pet>
    <pet>Fish</pet>
    </pets>
    </data>
    The code below will work

    Code:
    <script type="text/javascript">
    function loadXMLDoc(url) {
        // branch for native XMLHttpRequest object
        if (window.XMLHttpRequest) {
            req = new XMLHttpRequest();
            req.onreadystatechange = processReqChange;
            req.open("GET", url, true);
            req.send(null);
        // branch for IE/Windows ActiveX version
        } else if (window.ActiveXObject) {
            isIE = true;
            req = new ActiveXObject("Microsoft.XMLHTTP");
            if (req) {
                req.onreadystatechange = processReqChange;
                req.open("GET", url, true);
                req.send();
            }
        }
    }
    
    function processReqChange() {
        // only if req shows "loaded"
        if (req.readyState == 4) {
    	   
            // only if "OK"
            if (req.status == 200) {
    		  var result=req.responseXML;
              if (!result.documentElement && req.responseStream) {
                  result.load(req.responseStream);
              }  
    		var ev = result.getElementsByTagName('pet');	        
    		    document.getElementById("cd2").innerHTML=result.getElementsByTagName("pet").length;
                
    			
             } else {
                alert("There was a problem retrieving the XML data:\n" +
                    req.statusText);
             }
        }
    }
    </script>
    <body>
    
    <a href="#a" onclick="loadXMLDoc('data.xml')">Test</a>
    <div id="cd2"></div>
    </body>
    It returns 3 Ok

    But when I try to change data.xml like

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <data>
    <pets>
    <table border="1" cellpadding="0" cellspacing="0" style="border-collapse: collapse" bordercolor="#111111" width="100%" id="AutoNumber1">
           <tr>  
              <td><pet>Cat</pet></td>
    	   </tr>
    	   <tr>  
              <td><pet>Dog</pet></td>
    	   </tr>
    	   <tr>  
              <td><pet>Fish</pet></td>
    	   </tr>
        </table>
    </pets>
    </data>
    The script will return 0 Maybe it does not find <pet> element? What is different here? Please help me how to solve the problem Thanks in advance

  • #2
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well, first of all, XML is intended for data, not presentation, so trying to include a table within the XML data is highly discouraged.

    Secondly, it seems you're getting back status codes instead of your data. Looking at the XMLHttpRequest code you've got there, it looks broken. My suggestion? Simplify your life with something like moo.ajax... seriously.

    Try loading moo.ajax (as well as the required Prototype or Prototype Lite libraries), and give this code a whirl:
    Code:
    <script type="text/javascript">
    function loadXMLDoc(file) {
    	var options = {
    		method:'get',
    
    		onComplete:function(req) {
    			var xml = req.responseText;
    			$('cd2').innerHTML = xml.getElementsByTagName('pet').length;
    			return false;
    		}
    	};
    
    	ajax(file, options);
    }
    
    </script>
    
    <a href="" onclick="loadXMLDoc('data.xml')">Test</a>
    <div id="cd2"></div>

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #3
    New to the CF scene
    Join Date
    Feb 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yeah, it works now thank you thank you Skyzyx


  • #4
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Worked fine after a slight modification for running it via file: protocol, from local files here that is.

    Code:
    <html>
    <head>
      <title>loadXMLDoc sample</title>
      <script type="text/javascript">
    
        function loadXMLDoc(url) {
          try {req = new XMLHttpRequest();} catch(e) {
          try {req = new ActiveXObject("Microsoft.XMLHTTP");} catch(e) {}}
          if (req) {
            req.onreadystatechange = function() {
              if (req.readyState == 4) {
                var remoteServer = (/^http:/i.test(url) || /http:/.test(location.protocol));
                if (!remoteServer || req.status == 200) {
                  var result = req.responseXML;
                  if (!result.documentElement && req.responseStream) {
                    result.load(req.responseStream);
                  }
                  var pets = result.getElementsByTagName("pet");
                  document.getElementById("cd2").innerHTML = pets.length;
                } else {
                  alert("There was a problem retrieving the XML data:\n"+ req.statusText );
                }
              }
            };
            req.open("GET", url, true);
            req.send(null);
          } else {
            alert("Your browser does not support Ajax functionality.");
          }
        }
        
      </script>
    </head>
    <body>
      <a href="#cd2" onclick="loadXMLDoc('XMLFile1.xml')">Test</a>
      <div id="cd2"></div>
    </body>
    </html>
    Also, in response to Skyzyx, libraries are quite nice, but I think many people here want to LEARN the code so then they can know what is going on...?

    Just keep in mind, if you test these pages locally, you will not have receive some properties that would be set from remote server responses.

    req.status will return 0
    req.statusText will return "Unknown"
    even though you have valid responseXML/responseText

  • #5
    Regular Coder Skyzyx's Avatar
    Join Date
    Aug 2002
    Location
    Silicon Valley, CA
    Posts
    980
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by KC-Luck
    Also, in response to Skyzyx, libraries are quite nice, but I think many people here want to LEARN the code so then they can know what is going on...?
    I agree with you 100%. You should always take the time to learn how something works, as doing so will make you a better coder. However, once you understand how it works, then it becomes a matter of being as efficient as possible, IMHO.

    Prototype and moo.ajax are very efficient and very helpful tools for handling these kinds of things. Prototype especially has a TON of built-in functionality that truly makes life simpler for people working with particularly dynamic stuff.

    Hope this helps!

    Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.

  • #6
    Regular Coder
    Join Date
    Aug 2005
    Posts
    282
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yes, but even better, create your "own" library, when you have surpassed the error-prone libraries available
    All depends on your results, we need enterprise-level e-learning applications, so can't leave any methods up for mis-interpretations/exceptions

    Teach a man to code well, and feed them for life
    or, something like that...


  •  

    Posting Permissions

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