Lee-yoshi Posted January 20, 2012 Share Posted January 20, 2012 Hi!I've just started teaching myself XML properly to store data in instead of databases (Though it seems as if databases are better at this point). Suffice to say, it's not going very well... Here is my XML code: <?xml version="1.0" encoding="ISO-8859-1"?><!-- Diary example --><diary><entry date="20,01,2012"> <from>Lee</from> <content>Learnt PHP(LF)Learnt XML</content></entry><entry date="21,01,2012"> <from>Lee</from> <content>Learnt XML and got it to work</content></entry></diary> Here is my JavaScript: <script type = "text/javascript"> xmlDoc = loadXMLDoc( "testxml.xml" ); x = xmlDoc.getElementsByTagName( 'entry' ); for( i = 0 ; i < x.length; i++ ) { document.write( x[i].getAttribute( 'date' ) + " " + x[i].nodeType ); document.write( "<br />" ); document.write( x[i].childNodes[0].nodeName + " " + x[i].childNodes[0].nodeValue + " " + x[i].childNodes[0].nodeType ); document.write( "<br />" ); document.write( x[i].childNodes[1].nodeName + " " + x[i].childNodes[1].nodeValue + " " + x[i].childNodes[0].nodeType ); document.write( "<br />" ); }</script> Here is the output (In IE): 20,01,2012 1from null 1content null 121,01,2012 1from null 1content null 1 My question is... Why is it telling me that everything is null, when there's clearly text values stored in these nodes? And why is it telling me everything is an element - How do I make something a node? I'm so confused!! :-( Any help is greatly appreciated!!Thanks. [EDIT] Also, just for reference, the output in Firefox is: 20,01,2012 1#text 3from null 321,01,2012 1#text 3from null 3Why is there a difference and how can I control this??? :S Link to comment Share on other sites More sharing options...
boen_robot Posted January 20, 2012 Share Posted January 20, 2012 IE...8 and/or earlier I'm guessing? The key is in the childNodes collections...Only recently in IE9 does IE behave properly. Previously, empty whitespace nodes were not part of childNodes, whereas now they are.You can get a consistent behavior across browsers if you remove your whitespace nodes. e.g. instead of <?xml version="1.0" encoding="ISO-8859-1"?><!-- Diary example --><diary><entry date="20,01,2012"> <from>Lee</from> <content>Learnt PHP(LF)Learnt XML</content></entry><entry date="21,01,2012"> <from>Lee</from> <content>Learnt XML and got it to work</content></entry></diary> have it as <?xml version="1.0" encoding="ISO-8859-1"?><!-- Diary example --><diary><entry date="20,01,2012"><from>Lee</from><content>Learnt PHP(LF)Learnt XML</content></entry><entry date="21,01,2012"><from>Lee</from><content>Learnt XML and got it to work</content></entry></diary> That, or just don't use childNodes. Link to comment Share on other sites More sharing options...
Lee-yoshi Posted January 21, 2012 Author Share Posted January 21, 2012 Thanks for the reply! No, it is IE9 actually... which probably just make things worse! *sigh* Changed it anyway, but to no avail. What do you mean by not using childNodes? Link to comment Share on other sites More sharing options...
Ingolme Posted January 21, 2012 Share Posted January 21, 2012 Element nodes don't have a node value. The value you're looking for is in the text node within the element. x.childNodes[0].firstChild.nodeValue Link to comment Share on other sites More sharing options...
boen_robot Posted January 21, 2012 Share Posted January 21, 2012 Note also that being with IE9 doesn't guarantee you're not running in a compatibility mode of some sort.See if alert(document.documentMode) will give you "9" as the answer. If it's not, you're not running in standards mode => are using the old behavior, and will have to add a DTD to your (X)HTML. Link to comment Share on other sites More sharing options...
Lee-yoshi Posted January 22, 2012 Author Share Posted January 22, 2012 Excellent! Many thanks Ingolme, you solved my problem. Now I will continue on down the path of XML and see what will break me next! Also, thanks boen_robot, no I was running in compatibility mode ('5'). My code has now been DTD'd! Cheers Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.