473,473 Members | 4,176 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

6 New Member
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 2741
Logician
210 New Member
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
retrofill
6 New Member
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 New Member
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
retrofill
6 New Member
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 New Member
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
retrofill
6 New Member
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
retrofill
6 New Member
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 New Member
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
retrofill
6 New Member
:p thanks that works great
Mar 20 '07 #10

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

Similar topics

111
by: Retlak | last post by:
The recommended (on dozens of websites) and effective (works in Netscape, MSIE, Mozilla, probably others) way to detect if a browser has Javascript turned off is to put this in the <head>: ...
5
by: LRW | last post by:
(Sorry if this is a repost...my newsreader keeps crashing on the posting--I don't know if the message going out or not) For some reason this javascript just won't work in Firefox. It works fine...
4
by: Steph | last post by:
Hello, Can someone tell me the script to use for having a change on the same page when using checkbox function ? For example, i would to check one condition and display dynamically a button...
11
by: trinitypete | last post by:
Hi all, I have a user control that uses control literal to build a heading with a link, and a div containing links below. As the link heading is hit, I want to change the style of the div,...
7
by: Coder | last post by:
Hi I have the following code in java script, it is not giving proper output in FIREFOX but running fine in IE... can anybody help me out to make this run in FIREFOX . <script...
1
by: antonyliu2002 | last post by:
I never have luck in toggling divs with javascript, to which I am new. Not even with a single browser, let alone making it compatible with all major browsers. Also, I lack experience with client...
12
by: Manfred Kooistra | last post by:
I have a problem with some JavaScript code not working. I'm sure I've done something obviously stupid, but I can't for the live of me figure it out. Can someone please help? This is the XHTML...
12
by: usgog | last post by:
I have the following code in my html file. Somehow the "retry" div is always displaying and it has "addr has no properties" js error. What I want is: NOT display "retry" div and only display after...
6
by: adamscybot | last post by:
Here is the site in question: http://www.sws.vxcomputers.com/h2k/ Basically, on the left, you'll see a roster (them images) and you have to click the "Counter-Strike" link for it to load the...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.