www.webdeveloper.com
Results 1 to 3 of 3

Thread: Reading and displaying XML

  1. #1
    Join Date
    Dec 2011
    Posts
    1

    Reading and displaying XML

    Hello I'm new with AJAX and I'm trying to read an XML file with Javascript. It's supposed to display an alert message but it's not. What's wrong with my code?

    Thanks!


    HTML
    Code:
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>XML Exercise</title>
    <script type="text/javascript" src="xmlExercise.js"></script>
    </head>
    <body onLoad="getData()">
    
    </body>
    </html>
    Javascript (xmlExercise.js)
    Code:
    function getData()
    {
    	var Request = false;
    	var docElement, childOne, childTwo, textNode, targetData;
    	
    	if (window.XMLHttpRequest)
    	{
    		Request = new XMLHttpRequest();
    	}
    	else if (window.ActiveXObject)
    	{
    		Request = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	if (Request)
    	{
    		Request.open("GET", "library.xml");
    		Request.onreadystatechange = function()
    		{
    			if (Request.readyState == 4 && Request.status == 200)
    			{
    				var xmlDoc = Request.responseXML;
    				ClearOutWhiteSpace(xmlDoc);
    				docElement = xmlDoc.documentElement;
    				childOne = docElement.firstChild;
    				childTwo = childOne.nextSibling;
    				textNode = childTwo.firstChild;
    				targetData = textNode.nodeValue;
    				window.alert("There are " + targetData + " in the library.");
    			}
    		}
    	}
    	Request.send(null);
    }
    
    function ClearOutWhiteSpace(xmlFile)
    {
    	var i = 0;
    	for (i = 0; i < xmlFile.childNodes.length; i++)
    	{
    		var tag = xmlFile.chlidNodes[i];
    		if (tag.nodeType == 1)
    		{
    			ClearOutWhiteSpace(tag);
    		}
    		if ((tag.nodeType == 3) && (/^\s+$/.test(tag.nodeValue)))
    		{
    			xmlFile.removeChild(xmlFile.childNodes[i--]);
    		}
    	}
    }
    XML (library.xml)
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <library>
    	<History>57 Volumes</History>
    	<Biography>78 Volumes</Biography>
    	<Military>104 Volumes</Military>
    	<SciFi>61 Volumes</SciFi>
    </library>

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    AJAX won't work locally. Are you testing on a server?

  3. #3
    Join Date
    Jul 2008
    Location
    urbana, il
    Posts
    2,787
    ajax works fine locally, it's the infamous missing xhr.status on file:/// urls that prevents code (typically cut and pasted without deep understanding) from working.

    use sync (xhr's open() arguments[2] as false) or sniff just for xhr.responseText instead of using status.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles