SitePoint Sponsor |
|
User Tag List
Results 1 to 13 of 13
-
Dec 14, 2006, 03:35 #1
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
JavaScript / XML / IE and Firefox
I have some well-formed XML code, example below ...
Code:<?xml version="1.0" encoding="ISO-8859-1"?> <sensors> <sensor> <group>Current Sensors</group> <series>CSN</series> <subcat>Closed Loop</subcat> <listing>CSNA111</listing> <title>50 A nominal, 70 A range, 1000 turns</title> <desc>The CSN Series of closed loop current sensors are based on the principles of the Magnetoresistive or Hall effects, and the null balance or zero magnetic flux method (feedback system). The magnetic flux in the sensor core is constantly controlled at zero. The amount of current required to balance zero flux is the measure of the primary current flowing through the conductor, multiplied by the ratio of the primary to secondary windings. This closed loop current is the output from the device and presents an image of the primary current reduced by the number of secondary turns at any time. This current can be expressed as a voltage by passing it through a resistor.</desc> <features> <fitem>Measures ac, dc and impulse currents</fitem> </features> <apps> <appitem>Variable speed drives</appitem> </apps> </sensor> </sensors>
Code:<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> <html> <head> <title>XML tester</title> <script type="text/javascript"> var msie = (window.ActiveXObject) ? true : false; var moz = (document.implementation && document.implementation.createDocument) ? true : false; function importXML( xmlFile ) { if (moz) { xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.onload = showGroups; } else if (msie) { xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) showGroups(); }; } else { alert('Your browser can\'t handle this script'); return; } if ( !xmlDoc.load(xmlFile) ) { alert ("Failed to load XML data source!"); }; } function showGroups () { // This function creates a linked list of product groups var x = xmlDoc.getElementsByTagName('sensor'); var arrGroups = new Array (); var ctr = 0; for (i=0;i<x.length;i++) { var theNode = x[i].childNodes[0]; if (theNode.nodeType != 1) continue; var theGroup = theNode.firstChild.nodeValue; var groupExists = false; for (j=0;j<arrGroups.length;j++) { if (theGroup == arrGroups[j]) { groupExists = true; } } if (!groupExists) { arrGroups[ctr] = theGroup; ctr++; } } // Show the groups var newUL = document.createElement('UL'); for (i=0;i<arrGroups.length;i++) { var newLI = document.createElement('LI'); var newA = document.createElement('A'); newA.setAttribute('href','#'); newA.setAttribute('onClick','showListings ( "listings", ' + arrGroups[i] + ' )'); var theData = document.createTextNode(arrGroups[i]); newA.appendChild(theData); newLI.appendChild(newA); newUL.appendChild(newLI); } document.getElementById('groups').appendChild(newUL); } </script> </head> <body onload="importXML( 'sensors.xml' )"> <div id="groups"><h3>Product Groups</h3></div> </body> </html>
Anyone got any clues/pointers as to why this is?
-
Dec 14, 2006, 04:09 #2
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Additional info - it seems that Firefox is recognising the nodes as type=3 (i.e. nodeText) and not type=1 (nodeElement).
-
Dec 14, 2006, 05:12 #3
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Looks like whitespace in the XML file is to blame - FF is reacting correctly by recognising whitespace as nodeText. I stripped the whitespace and it now works.
Any good ideas on how to import an XML file and strip the whitespace in JavaScript whilst still retaining the XML properties?
(An alternative is to strip and create arrays representing the nodes).
-
Dec 14, 2006, 13:05 #4
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Your code should not assume that text nodes will not be present. When parsing a DOM always skip over any node with nodeType!=1 (if you are only looking for Element nodes).
You can use wrapper functions which take care of the grunt work for you, for example: xFirstChild, xNextSib, etc.Cross-Browser.com, Home of the X Library
-
Dec 15, 2006, 04:47 #5
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Thanks for the pointers Mike. That looks like a very nice library.
-
Dec 18, 2006, 06:21 #6
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Mike,
I'm having a problem with your X library, specifically with the xGetElementsByTagName function. In Firefox it works perfectly - in IE6 I get the error Wrong number of arguments or invalid property assignment. The line generating the error is
Code:if (p.getElementsByTagName) { // DOM1
Code:var x = xGetElementsByTagName ('group', xmlDoc);
Any ideas what's happening? As I say, FF1.5 works perfectly.
-
Dec 18, 2006, 08:30 #7
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
I should also say that doing an alert (p) just before that if() line shows [object] in IE6 and [object XMLDocument] in FF1.5.
-
Dec 19, 2006, 11:10 #8
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
siteguru, sorry I don't have much time for a response.
It sounds like your data is not being served as "text/xml". Is it?
Read my comments in the "description" on this page.Cross-Browser.com, Home of the X Library
-
Dec 20, 2006, 05:41 #9
- Join Date
- Oct 2002
- Location
- Scotland
- Posts
- 3,631
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
But this all relates to remote (URL path) files. I'm trying to run the HTML page as a filesystem file (e.g. run from CD) wherein the XML data source is in the same folder as the HTML file.
Any other clues/advice?
-
Dec 20, 2006, 08:42 #10
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Hi siteguru, I've made a change to xGetElementsByTagName because of something strange in IE:
typeof xmlDoc.getElementsByTagName is 'unknown', but the function does exist. More IE crap
Copy the source from the link above and paste over the contents of the file: "x/lib/xgetelementsbytagname.js".Cross-Browser.com, Home of the X Library
-
Dec 21, 2006, 02:59 #11
Thanks Mike. I had come to the same conclusion and got it to work by putting the IE part first (p.all etc.) but your update is more elegant.
Thanks for your help with this - much appreciated.
All I need to figure out now is how to get JS to dynamically put an image into the Specifications area, but that's a different question.
PS - yes this is siteguru. For some weird reason, from mid-morning yesterday I have no longer been able to access my siteguru account. I've tried the password reset but the emails haven't been coming through. (The first one did, but when I followed the link the follow-up didn't and then the site said the request was >24 hours old! So I've created this new account.)
-
Dec 21, 2006, 09:36 #12
Actually, I've got that sorted. It's all working hunky-dory!
-
Dec 21, 2006, 11:06 #13
- Join Date
- Dec 2002
- Location
- Alabama, USA
- Posts
- 2,560
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Cross-Browser.com, Home of the X Library
Bookmarks