Go Back   CodingForums.com > :: Client side development > XML

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 08-28-2002, 04:05 AM   PM User | #1
Evlich
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Evlich is an unknown quantity at this point
Cross Browser XML Parsing...

Hello, can anyone help me? I need to create a way to parse XML on the client-side in as many browsers as possible. Can someone give me a link or something, thanks a lot.
Evlich is offline   Reply With Quote
Old 08-28-2002, 05:56 PM   PM User | #2
Bosko
Regular Coder

 
Join Date: Jun 2002
Location: The Netherlands
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Bosko is an unknown quantity at this point
http://www.xs4all.nl/~ppk/js/importxml.html
Bosko is offline   Reply With Quote
Old 08-29-2002, 04:14 AM   PM User | #3
Evlich
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Evlich is an unknown quantity at this point
Is there a way to do this so that I can use an XSL transform sheet instead of doing all this javascript stuff? Thanks a lot.
~evlich
Evlich is offline   Reply With Quote
Old 10-29-2002, 04:31 AM   PM User | #4
Evlich
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Evlich is an unknown quantity at this point
Are there any ways to do it with XSL stylesheet?
Evlich is offline   Reply With Quote
Old 10-29-2002, 09:44 AM   PM User | #5
mpjbrennan
Regular Coder

 
Join Date: Jun 2002
Location: Newcastle, England
Posts: 178
Thanks: 0
Thanked 0 Times in 0 Posts
mpjbrennan is an unknown quantity at this point
XSLT1.0 will work in IE6, NS7(I believe) and Mozilla 1.0+ only

patrick
mpjbrennan is offline   Reply With Quote
Old 11-06-2002, 04:47 AM   PM User | #6
Evlich
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Evlich is an unknown quantity at this point
Here is ultimately what I want to do:
Write a function
Code:
function loadXMLSource("XML_File_Name", "XSL_File_Name", "HTML element that I want to put the output into")
{
...
}
and I want to know what I need to put in for the "..." I already know the stuff with the MSXML engine and stuff like that, but logic is telling me that since Microsoft didn't make Netscape or any other browser, that MSXML is not supported in the other browsers. Is there a way to do this? Thanks a lot.
Evlich is offline   Reply With Quote
Old 11-06-2002, 08:11 AM   PM User | #7
mpjbrennan
Regular Coder

 
Join Date: Jun 2002
Location: Newcastle, England
Posts: 178
Thanks: 0
Thanked 0 Times in 0 Posts
mpjbrennan is an unknown quantity at this point
Jason posted this some time ago and it works for IE6 and Mozilla:

<html>
<head>
<title>Importing and Manipulating XML</title>
<script language="javascript">

/*************************************
DECLARE GLOBAL VARIABLES
*************************************/
var c
var d='';
var e='';
var f='';
var g='';
var xml_doc

/*************************************
MOZILLA - EXTRACT NODE INFORMATION
*************************************/
function readFileMoz()
{
node_list = xml_doc.getElementsByTagName("RESTAURANT")
for (i = 0; i <= node_list.length-1; i++)
{
c = node_list[i].childNodes[1].childNodes[0].nodeValue
d = node_list[i].childNodes[3].childNodes[0].nodeValue
//e = node_list[i].childNodes[5].nodeValue
//f = node_list[i].childNodes[7].nodeValue
//g = node_list[i].childNodes[9].nodeValue
alert("C = " + c + " D = " + d)
}
}


/*************************************
INTERNET EXPLORER - EXTRACT NODE INFORMATION
*************************************/
function readFileIE()
{
node_list = xml_doc.getElementsByTagName("RESTAURANT")

for (i = 0; i <= node_list.length-1; i++)
{
c = node_list[i].childNodes[0].childNodes[0].nodeValue
d = node_list[i].childNodes[1].childNodes[0].nodeValue
//e = node_list[i].childNodes[2].nodeValue
//f = node_list[i].childNodes[3].nodeValue
//g = node_list[i].childNodes[4].nodeValue
alert("C = " + c + " D = " + d)
}

}


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

if (moz)
{
xml_doc = document.implementation.createDocument("", "", null)
xml_doc.onload = readFileMoz
}

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

xml_doc.load(zFileName)

}

importXML('cust.xml');

</script>


</head>
<body>
</body>
</html>

This is the xml file which goes with it:

<?xml version="1.0"?>

<LIST>

<RESTAURANT>
<NAME>A Special Place</NAME>
<ADDRESS>3124 Williamsburg Dr.</ADDRESS>
</RESTAURANT>

<RESTAURANT>
<NAME>Al Fresco Cafe</NAME>
<ADDRESS>3398 El Camino Real</ADDRESS>
</RESTAURANT>

</LIST>

patrick
mpjbrennan is offline   Reply With Quote
Old 11-06-2002, 10:27 PM   PM User | #8
Evlich
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Evlich is an unknown quantity at this point
Ok, but what heppens if I don't exactly know what the tags in it are going to be. For instance from this I may load a list of numbers, or I may load a paragraph which would each have different tags?
Evlich is offline   Reply With Quote
Old 11-07-2002, 01:09 PM   PM User | #9
brothercake
Senior Coder


 
Join Date: Jun 2002
Location: near Oswestry
Posts: 4,508
Thanks: 0
Thanked 0 Times in 0 Posts
brothercake is an unknown quantity at this point
You must have some idea of what tags are going to be there; you can't parse an xml doc without referring to the tags. That's what DTD is for.
brothercake is offline   Reply With Quote
Old 11-07-2002, 01:23 PM   PM User | #10
Evlich
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Evlich is an unknown quantity at this point
Yeah, the thing is I could use this to import xml files that work of different DTD's correct. I would have a different stylesheet for each one that would define the way to manipulate only that data. For instance, one of the things that I am planning on doing this to is the menu on my site. I wanted to do a heirarchal menu and was going to use
<menu>
<submenu>
<item>
with various attributes. However, I would like to use the same function to inport table data, like a table of contents which might be like this:
<toc>
<section>
<title href="">
<pageN>
</section>
Is it possible to parse them off the same function?
Evlich is offline   Reply With Quote
Old 11-28-2002, 09:02 AM   PM User | #11
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
Evlich,

The example that mpjbrennan used was when someone was trying to explain all of this stuff to me. Listen... for your own sake, do not use this "node_list[i].childNodes[0].childNodes[0].nodeValue" stuff. Use XML tag names instead. That way, you don't have to worry about IE/Moz whitespace crap...

I'd suggest using something like this:

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.

--Sky
__________________

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
Old 11-28-2002, 04:17 PM   PM User | #12
Evlich
New Coder

 
Join Date: Jun 2002
Posts: 72
Thanks: 0
Thanked 0 Times in 0 Posts
Evlich is an unknown quantity at this point
It looks great. Only one question. You wrote this function:
Code:
/*************************************
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 />');
	}
}
I see what you are doing, you are getting the data from the data elements and putting them in an array and stuff like that, but is it possible to instead to saying:
Code:
document.write(xmlFileData[x] + '<br />');
to say something that would apply a template in an xsl file to the node?
Thanks a lot.
~evlich
Evlich 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 On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:52 PM.


Advertisement
Log in to turn off these ads.