Jump to content

Everything is null :(


Lee-yoshi
 Share

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 Edited by Lee-yoshi
  • Like 2
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...