469,963 Members | 1,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

My javascript display is disapearing! using firstchild.data for xml

Hey i've got a problem with printing out an XML document:

XML doc:
http://www.deniseburrows.pwp.blueyonder.co.uk/javascript/data.xml


If i remove the comments from the lines that add the 'publisher' and 'year' nodes, nothing is displayed on the page! :/

the code is as follows:


Expand|Select|Wrap|Line Numbers
  1.     function doDisplay(xmlDoc)
  2.     { var books = xmlDoc.getElementsByTagName('book');
  3.       var txt = '';
  4.       for (var aBook = 0;  aBook < books.length; aBook ++)
  5.       { var book = books[aBook];
  6.         txt += '<p>';
  7.         txt += book.getElementsByTagName('title')[0].firstChild.data;
  8.         txt += ', ';
  9.         txt += book.getElementsByTagName('author')[0].firstChild.data;
  10.         txt += ', ';
  11.     //txt += book.getElementsByTagName('publisher')[0].firstChild.data;
  12.         txt += ', ';
  13.         //txt += book.getElementsByTagName('year')[0].firstChild.data;
  14.         txt += ', ';
  15.         txt += book.getElementsByTagName('condition')[0].firstChild.data;
  16.         txt += ', ';
  17.         txt += book.getElementsByTagName('price')[0].firstChild.data;
  18.  
  19.         txt += '</p>\n';
  20.       }
  21.       displayDiv.innerHTML = txt;// + anothertxt;
  22.     }
Mar 18 '07 #1
9 2632
Logician
210 100+
Hey i've got a problem with printing out an XML document:

XML doc:
http://www.deniseburrows.pwp.blueyonder.co.uk/javascript/data.xml
Not all of your records contain publisher or year tags, and you are not testing for their presence. This must be causing errors indicated in the console. Try this function:
Expand|Select|Wrap|Line Numbers
  1. function getElemText(elem, tag)
  2. {
  3.  var arr, retVal="";
  4.  
  5.  if( (arr=elem.getElementsByTagName(tag) ) && arr[0] && arr[0].firstChild!='undefined' )
  6.   retVal=arr[0].firstChild.data;
  7.  
  8.  return retVal;
  9. }
  10.  
  11. /* Call like this */
  12.  
  13. txt += getElemText(book, 'publisher');
  14.  
  15.  
Mar 18 '07 #2
I tried this, didn't seem to work. From what i can tell it is not getting the 'cd' records; they are not tested in that function, only the 'book' records are -

and they all have the publisher and year elements.

not sure if i'm right there, but i've tried doing those checks and it still didn't work for some reason.
Mar 18 '07 #3
Logician
210 100+
I tried this, didn't seem to work. From what i can tell it is not getting the 'cd' records; they are not tested in that function, only the 'book' records are -

and they all have the publisher and year elements.

not sure if i'm right there, but i've tried doing those checks and it still didn't work for some reason.
That was just a usage example . I meant for you to use that function multiple times, to replace all the assignments that you have currently.
Mar 18 '07 #4
I changed the pseudo code you wrote and put a check for every element, but it didn't work. Not sure what to do now.
Mar 19 '07 #5
Logician
210 100+
I changed the pseudo code you wrote and put a check for every element, but it didn't work. Not sure what to do now.
Can you show your code as it stands, and are you getting any console errors?
Mar 19 '07 #6
Expand|Select|Wrap|Line Numbers
  1.     <script type="text/javascript" src="xhrlib.js"></script>
  2.     <script type="text/javascript">
  3.  
  4.     var displayDiv;
  5.     var books;
  6.     var CDs;
  7.  
  8.     function init(displayDivID)
  9.     { displayDiv = document.getElementById(displayDivID);
  10.         requestXML('data.xml', setup,true);
  11.     //doBookDisplay();
  12.     }
  13.  
  14.     function setup(xmlDoc)
  15.     { 
  16.     books = xmlDoc.getElementsByTagName('book');
  17.     CDs =   xmlDoc.getElementsByTagName('cd');
  18.     }
  19.  
  20.  
  21.     function doBookDisplay()
  22.     {
  23.       var txt = '';
  24.       displayDiv.innerHTML = '';
  25.       for (var aBook = 0;  aBook < books.length; aBook ++)
  26.       { var book = books[aBook];
  27.  
  28.         txt += '<p>';
  29.  
  30.     if(book.getElementsByTagName('title')){
  31.             txt += book.getElementsByTagName('title')[0].firstChild.data;
  32.     }
  33.  
  34.         txt += ', ';
  35.  
  36.     if(book.getElementsByTagName('author')){
  37.             txt += book.getElementsByTagName('author')[0].firstChild.data;
  38.             txt += ', ';
  39.     }
  40.     if(book.getElementsByTagName('publisher')){
  41.         //txt += book.getElementsByTagName('publisher')[0].firstChild.data;
  42.         //txt += "working--------------";
  43.         txt += ', ';
  44.         //this does not work- no idea why
  45.     }
  46.  
  47.  
  48.     if(book.getElementsByTagName('year')){
  49.             //txt += book.getElementsByTagName('year')[0].firstChild.data;
  50.             txt += ', ';
  51.         //this does not work- no idea why
  52.     }
  53.  
  54.     if(book.getElementsByTagName('condition')){
  55.             txt += book.getElementsByTagName('condition')[0].firstChild.data;
  56.         txt += ', ';
  57.     }
  58.  
  59.  
  60.  
  61.     if(book.getElementsByTagName('price')){
  62.         txt += book.getElementsByTagName('price')[0].firstChild.data;
  63.     }
  64.  
  65.         txt += '</p>\n';
  66.  
  67.       }
  68.       displayDiv.innerHTML = txt;// + anothertxt;
  69.     }
  70.  
  71.  
  72.  
  73.     function doCDDisplay(xmlDoc)
  74.     {
  75.       var txt = '';
  76.       displayDiv.innerHTML = 'cv';
  77.       for (var aCD = 0;  aCD < CDs.length; aCD ++)
  78.       { var cd = CDs[aCD];
  79.  
  80.         txt += 'lkjlkj<p>';
  81.  
  82.     if(cd.getElementsByTagName('title')){
  83.             //txt += cd.getElementsByTagName('title')[0].firstChild.data;
  84.     }
  85.  
  86.         txt += ', ';
  87.  
  88.     if(cd.getElementsByTagName('artist')){
  89.             //txt += cd.getElementsByTagName('artist')[0].firstChild.data;
  90.             txt += ', ';
  91.     } 
  92.     if(book.getElementsByTagName('condition')){
  93.             //txt += book.getElementsByTagName('condition')[0].firstChild.data;
  94.         txt += ', ';
  95.     }       
  96.     if(cd.getElementsByTagName('year')){
  97.             //txt += cd.getElementsByTagName('year')[0].firstChild.data;
  98.             txt += ', ';
  99.         //this does not work- no idea why
  100.     }       
  101.  
  102.     if(cd.getElementsByTagName('price')){
  103.         //txt += cd.getElementsByTagName('price')[0].firstChild.data;
  104.     }
  105.  
  106.         txt += '</p>\n';
  107.  
  108.       }
  109.       displayDiv.innerHTML = txt;// + anothertxt;
  110.     }
  111.     function here(){
  112.         displayDiv.innerHTML = 'cleared';
  113.     }
  114.  
  115.     </script>
http://www.deniseburrows.pwp.blueyon...uk/javascript/

Theres no error messages being displayed.
Press the 'book' button.

Its a mess as you can see, but if i take the comments off the areas actually displaying the publisher and year within the book function, nothing displays when the button is pressed.
Mar 19 '07 #7
I just added this within the for loop (after the declaration of 'book')

alert( book.getElementsByTagName('publisher').length );

0 is supposed to show an error in the book declaration

and the error check returns 1 except for once, which means theres a problem. Not sure what it is though.

Anybody got any suggestions?
Mar 19 '07 #8
Logician
210 100+
Its a mess as you can see, but if i take the comments off the areas actually displaying the publisher and year within the book function, nothing displays when the button is pressed.
You didn't use the code I gave you.
The test
Expand|Select|Wrap|Line Numbers
  1.  if( getElementsByTagName('xx') )
does not return false if there are no such tags.

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  2.           "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  3. <html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
  4.   <head>
  5.     <meta http-equiv="Content-Type"
  6.           content="text/html; charset=ISO-8859-1"/>
  7.     <title>Example 16-1 - (Taken from Peter Coxshead's Example Material and edited for Homework)       </title>
  8.     <script type="text/javascript" src="xhrlib.js"></script>
  9.     <script type="text/javascript">
  10.  
  11.     function getElemText(elem, tag)
  12.     {
  13.      var arr, retVal="";
  14.  
  15.      if( (arr=elem.getElementsByTagName(tag) ) && arr[0] && arr[0].firstChild!='undefined' )
  16.       retVal=arr[0].firstChild.data;
  17.  
  18.      return retVal;
  19.     }
  20.  
  21.     var displayDiv;
  22.     var books;
  23.     var CDs;
  24.  
  25.     function init(displayDivID)
  26.     { displayDiv = document.getElementById(displayDivID);
  27.       requestXML('data.xml', setup,true);
  28.  
  29.     }
  30.  
  31.     function setup(xmlDoc)
  32.     { 
  33.    books = xmlDoc.getElementsByTagName('book');
  34.    CDs =   xmlDoc.getElementsByTagName('cd');
  35.     }
  36.  
  37.  
  38.  function doBookDisplay()
  39.  {
  40.   var txt = '', t;
  41.   displayDiv.innerHTML = '';
  42.  
  43.  
  44.    for (var aBook = 0;  aBook < books.length; aBook ++)
  45.    {
  46.     var book = books[aBook];
  47.  
  48.     txt += '<p>';
  49.  
  50.  
  51.     txt += getElemText(book, 'title');   
  52.     txt += ', ';
  53.     txt += getElemText(book, 'author');  
  54.     txt += ', ';
  55.  
  56.     if( (t=getElemText(book, 'publisher'))!='')
  57.     {
  58.      txt += t + "working--------------"
  59.      txt += ', ';
  60.     }
  61.  
  62.     if( (t=getElemText(book, 'year'))!='')
  63.     {
  64.      txt += t;
  65.      txt += ', ';
  66.     }
  67.  
  68.     if( (t=getElemText(book, 'condition'))!='')
  69.     {
  70.      txt += t;
  71.      txt += ', ';
  72.     }
  73.  
  74.     t+=getElemText(book, 'price');
  75.     txt += '</p>\n';
  76.  
  77.    }
  78.       displayDiv.innerHTML = txt;// + anothertxt;
  79.  }
  80.  
  81.  
  82.  
  83.  function doCDDisplay(xmlDoc)
  84.  {
  85.   var txt = '', t;
  86.  
  87.   displayDiv.innerHTML = 'cv';
  88.  
  89.   for (var aCD = 0;  aCD < CDs.length; aCD++)
  90.   { 
  91.    var cd = CDs[aCD];
  92.  
  93.    txt += '<p>';
  94.  
  95.    if((t=getElemText(cd, 'condition'))!='')
  96.    {
  97.     txt+=t;
  98.     txt += ', ';
  99.    }
  100.  
  101.    if((t=getElemText(cd, 'artist'))!='')
  102.    {
  103.     txt+=t;
  104.     txt += ', ';
  105.    }  
  106.  
  107.    if((t=getElemText(cd, 'year'))!='')
  108.    {
  109.     txt+=t;
  110.     txt += ', ';
  111.    }       
  112.  
  113.    if((t=getElemText(cd, 'price'))!='')
  114.    {
  115.     txt+=t;
  116.     txt += ', ';
  117.    }
  118.  
  119.    txt += '</p>\n';
  120.  
  121.   }
  122.  
  123.   displayDiv.innerHTML = txt;
  124.  }
  125.  
  126.  function here()
  127.  {
  128.   displayDiv.innerHTML = 'cleared';
  129.  }
  130.  
  131.      </script>
  132.   </head>
  133.  
  134.   <body onload="init('display');">
  135.     <h1>Books and Books</h1>
  136.     <p>The data below was obtained from <a href="data.xml">data.xml</a> on the same web server as  this page.</p>
  137.  
  138.     <input type="button" value="Books" onclick="doBookDisplay()" />
  139.     <input type="button" value="CDs"   onclick="doCDDisplay()" />
  140.  
  141.     <div id="display" onload="doBookDisplay();"></div>
  142.  
  143.     <input type="button" value="Next" onclick="step('i01')" />
  144.   </body>
  145. </html>
  146.  
  147.  
I'll let you decide the re-write fee.
Mar 19 '07 #9
:p thanks that works great
Mar 20 '07 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by LRW | last post: by
4 posts views Thread by Steph | last post: by
11 posts views Thread by trinitypete | last post: by
7 posts views Thread by Coder | last post: by
1 post views Thread by antonyliu2002 | last post: by
12 posts views Thread by Manfred Kooistra | last post: by
12 posts views Thread by usgog | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.