Jump to content

Everything is null :(


Lee-yoshi

Recommended Posts

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 3
Why is there a difference and how can I control this??? :S
Link to comment
Share on other sites

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...