Go Back   CodingForums.com > :: Client side development > JavaScript programming > Post a JavaScript

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-28-2002, 09:09 AM   PM User | #1
Skyzyx
Regular Coder

 
Skyzyx's Avatar
 
Join Date: Aug 2002
Location: Silicon Valley, CA
Posts: 980
Thanks: 0
Thanked 0 Times in 0 Posts
Skyzyx is on a distinguished road
Importing XML data using JS/DOM. IE5/Gecko 1.0-compliant.

If you have an XML file named "names.xml" that looks like this,
Code:
<?xml version="1.0"?>
<DOC>
	<DATA>Bobby</DATA>
	<DATA>Fredo</DATA>
	<DATA>Miguel</DATA>
</DOC>
You'll want your script to do something like this:

Code:
/*************************************
This function loads the file when run.
*************************************/
function importXML(zFileName)
{
	var xmlDoc;
	var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
	var ie = (typeof window.ActiveXObject != 'undefined');

	if (moz)
	{
		xmlDoc = document.implementation.createDocument("", "", null)
		xmlDoc.onload = readXML;
	}

	else if (ie)
	{
		xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.onreadystatechange = function()
		{
			if (xmlDoc.readyState == 4) setTimeout(readXML,0);
		}
	}

	xmlDoc.load(zFileName);
}


/*************************************
This is the function that is run when importXML()
is called...
*************************************/
function readXML()
{
	var xmlFile=xmlDoc.getElementsByTagName("DOC"); // There is only one DOC tag, so this is a variable instead of an array.
	var xmlFileData=xmlFile.getElementsByTagName("DATA"); // There is more than one DATA tag, so this becomes an array.  The first DATA tag (value is Bobby) can be accessed as xmlData[0], the next xmlData[1], and so on.

	for (x=0; x < xmlFileData.length; x++) // Since this is an array...
	{
		document.write(xmlFileData[x] + '<br />');
	}
}


/*************************************
Actually import the document.
*************************************/
var loc=location.href;
if (loc.indexOf('.htm?') != -1)
{
	var impurl=loc.split('?');
	importXML(impurl[1]);
}
else alert('You need to specify an XML document to load.');
With this, you can use the same HTML page to load different XML files one at a time. For example, you would pull up a new page by going to "http://www.domain.com/filename.htm?names.xml". After the filename.htm, you would add a ? followed by the name of the XML file, not quotated.

Granted, you can do much more with the readXML() function than this, but this is just something that you can look at to figure out where you can go from here.
__________________

Creator of SimplePie and Tarzan AWS, co-founder of WarpShare, co-built the Y! Messenger website, usability-focused, and an INFJ personality.
Skyzyx is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:18 AM.


Advertisement
Log in to turn off these ads.