Helpful Information
 
 
Category: XML
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.

http://www.xs4all.nl/~ppk/js/importxml.html

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

Are there any ways to do it with XSL stylesheet?

XSLT1.0 will work in IE6, NS7(I believe) and Mozilla 1.0+ only

patrick

Here is ultimately what I want to do:
Write a function


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.

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

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?

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.

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,

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,


<?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:



/*************************************
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

It looks great. Only one question. You wrote this function:


/*************************************
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:


document.write(xmlFileData[x] + '<br />');

to say something that would apply a template in an xsl file to the node?
Thanks a lot.
~evlich










privacy (GDPR)