MozillaZine

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.


#9 Re: Reply

by irongut

Tuesday January 13th, 2004 11:40 AM

You are replying to this message

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); }

</script>

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.