Read and Process XML Data Using JavaScript in MozillaThursday January 8th, 2004Sohail 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. 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. http://devedge.netscape.com/viewsource/2003/soap/01/ is an article I wrote that explains the basics - it isn't that hard. nice. maybe this will be a good starting point for those who want to stream from the database.modev.org xml file we use at extensionroom and firebird help. 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. 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 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. mawrya 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? 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. 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. 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 Moby rtvkuijk Monday January 19th, 2004 4:00 PM www.oxygenxml.com 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"); } |