We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,445 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

How to read XML through Javascript

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

<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

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

<?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>
4
Contributors
8
Replies
6 Days
Discussion Span
1 Year Ago
Last Updated
9
Views
sanityhien
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

I'd have to look this up myself, however, it is a better idea to use JSON instead of XML for AJAX.

stbuchok
Practically a Posting Shark
882 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2

I just realized how funny that sounds considering what AJAX stands for, however, look at using JSON.

stbuchok
Practically a Posting Shark
882 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2

ok thanks, i'll look into JSON! but if any of you have an idea why this is not working, please tell, thank you :)

sanityhien
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

How are you serving the documents?

Airshow
WiFi Lounge Lizard
Moderator
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10

How are you serving the documents?

all documents are in the same folder. The problem is I don't know the problem. Would you know what's wrong with my code? I can't seem to read any kind of XML. Thanks

sanityhien
Newbie Poster
3 posts since Dec 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

OK, but how are you serving the documents; Apache, IIS, Windows localHost?

Airshow
WiFi Lounge Lizard
Moderator
2,782 posts since Apr 2009
Reputation Points: 370
Solved Threads: 389
Skill Endorsements: 10

Airshow's tag line says it all

50% of the solution lies in accurately describing the problem!

I would also say it could be modified to say the following:

40% of the solution lies in accurately describing the problem!
40% of the solution lies in understanding the problem!
20% of the solution lies in good coffee!

stbuchok
Practically a Posting Shark
882 posts since May 2011
Reputation Points: 138
Solved Threads: 124
Skill Endorsements: 2

I just read out the code, but didn't find any problems with it.
I'm pretty sure your ajax call is not being served at all.
You may have ran into some sort of a security issue.

Troy III
Master Poster
782 posts since Jun 2008
Reputation Points: 140
Solved Threads: 100
Skill Endorsements: 5

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1321 seconds using 2.8MB