Read and Process XML Data Using JavaScript in Mozilla

Thursday January 8th, 2004

Sohail Mirza wrote in to tell us that SitePoint has an article about XML and Javascript in Mozilla. The tutorial describes how to use JavaScript to read in an XML file and extract data from it. It follows on from a previous SitePoint article discussing how to process XML in a similar way using Internet Explorer.

#1 Mozilla SOAP

by pbreit

Thursday January 8th, 2004 12:24 PM

I've tried a few times to make SOAP calls in Mozilla with little success. Seems like this should be pretty simple. I can't tell if it's a Moz problem or because SOAP is overly complicated.

#3 Re: Flash

by doron

Thursday January 8th, 2004 12:46 PM is an article I wrote that explains the basics - it isn't that hard.

#2 cool

by jedbro

Thursday January 8th, 2004 12:43 PM

nice. maybe this will be a good starting point for those who want to stream from the xml file we use at extensionroom and firebird help.

#4 Screenshots

by briank

Thursday January 8th, 2004 1:06 PM

His screenshots show a build id of 2002053012! Either he wrote the article a long time ago and has been sitting on it, or he is in bad need to download an update.

#5 xml+css+javascript

by rtvkuijk

Thursday January 8th, 2004 2:37 PM

I think sombody (maybe me if I can find the time) to create an example that just uses xml with css for styling and javascript for dynamic (dxml?) to create a ticker without using all this document.write stuff

#6 old code

by mawrya

Thursday January 8th, 2004 5:29 PM

This is an example for pre-moz1.4. The .async property for loading xml docs has been functional since moz1.4 but it didn't show up in the example, which uses the old onload handler--good place to start, but there's a better way now.


#8 Reply

by napolj2

Friday January 9th, 2004 10:59 PM

I've tried working through the examples in the article, but the Javascript console keeps telling me that 'readXML' isn't defined. I guess this function is no longer supported. What is the new way to load the xml document?

#9 Re: Reply

by irongut

Tuesday January 13th, 2004 11:40 AM

The article is badly written and you need to alter the code a bit. Try this:

<script type="text/javascript" language="JavaScript">

var xmlDoc;

function LoadXML(xmlFile) { xmlDoc = document.implementation.createDocument("", "doc", null); xmlDoc.onload = ProcessXML; xmlDoc.load(xmlFile); }

function ProcessXML() { var companies = xmlDoc.getElementsByTagName("company"); var employees = companies[0].getElementsByTagName("employee"); document.writeln(employees[0].firstChild.nodeValue); document.writeln("<br>"); document.writeln(employees[1].firstChild.nodeValue); }


Call loadXML("yourfile.xml") to start the process. You don't need to call processXML(), it will be called when the file has finished loading.

I find that the browser never finishes loading the page that calls loadXML(). You can even spot this problem in his screenshot.

#10 Re: old code

by irongut

Tuesday January 13th, 2004 11:46 AM

Can you point us at docs or an article using .async?

#12 Re: old code

by shaneski

Monday February 2nd, 2004 5:36 PM

i am trying to look for a tutorial in this subject but i cannot find any (just the sample from above but i can't make it run). i'm just about to give up on scripting xml. do you know other articles or tutorials somewhere? please... thanks in advance.

#13 Re: Re: old code

by shaneski

Monday February 2nd, 2004 5:44 PM

i did it. it runs. i just have to use the sample xml in the article. nice. but i still have to tinker with it to use with my own xml... coool...

#7 Any of you guys know of a XSLT debugger for Moz?

by duffbeer703

Friday January 9th, 2004 7:44 PM

I love the idea of using Moz to transform XML documents... but its a real pain to troubleshoot problems. What's a good way to debug this stuff?

#11 Re: Any of you guys know of a XSLT debugger for Mo

by rtvkuijk

Monday January 19th, 2004 4:00 PM

#14 Diferent reading IE and Mozilla

by eurofins

Tuesday February 24th, 2004 5:41 AM

I'm currently using 'DHTML coolMenus - from' and encontered a problem reading the XML file when I used IE or Mozilla. and found following solution after reading 'Use XML to drive a DHTML menu' at I replaced in the original code the makeMenu hardcoded part by a function and added the crossbrowser hints of the article

function ProcessXML() { var menuItems = xmlDoc.getElementsByTagName("menuitem"); var e; var nodeid; var parentid; var labelTxt; var linkTxt; var i; //for (var i=0;i < 6 ; i++)

for (var i=0;i < menuItems.length ; i++) { // assign each element of the XML file to a variable e=menuItems[i]; nodeid= 'm' + e.getElementsByTagName("node")[0]; parentid= 'm' + e.getElementsByTagName("parent")[0]; labelTxt=e.getElementsByTagName("label")[0]; linkTxt =e.getElementsByTagName("link")[0]; //oM.makeMenu('<id>','<parent id>','<label>','<url>') ; /****************************************** myCoolMenu.makeMenu(name, parent_name, text, link, target, width, height, regImage, overImage, regClass, overClass , align, rows, nolink, onclick, onmouseover, onmouseout) */ oCMenu.makeMenu(nodeid,parentid,labelTxt,linkTxt,'target','60') } oCMenu.construct() }

var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined'); var ie = (typeof window.ActiveXObject != 'undefined');

if (ie) { // the IE reading is slightly different from the NS/Mozilla var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.load("js/menu.xml") ProcessXML(xmlDoc); }

else { // the mozilla way xmlDoc = document.implementation.createDocument("", "doc", null); xmlDoc.onload = ProcessXML; xmlDoc.load("js/menu.xml"); }