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.


#14 Diferent reading IE and Mozilla

by eurofins

Tuesday February 24th, 2004 5:41 AM

You are replying to this message

I'm currently using 'DHTML coolMenus - from coolmenus.dhtmlcentral.com' 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 <http://builder.com.com/5100-6371-5073484.html>. 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].firstChild.data; parentid= 'm' + e.getElementsByTagName("parent")[0].firstChild.data; labelTxt=e.getElementsByTagName("label")[0].firstChild.data; linkTxt =e.getElementsByTagName("link")[0].firstChild.data; //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"); }