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: -
function doDisplay(xmlDoc)
-
{ var books = xmlDoc.getElementsByTagName('book');
-
var txt = '';
-
for (var aBook = 0; aBook < books.length; aBook ++)
-
{ var book = books[aBook];
-
txt += '<p>';
-
txt += book.getElementsByTagName('title')[0].firstChild.data;
-
txt += ', ';
-
txt += book.getElementsByTagName('author')[0].firstChild.data;
-
txt += ', ';
-
//txt += book.getElementsByTagName('publisher')[0].firstChild.data;
-
txt += ', ';
-
//txt += book.getElementsByTagName('year')[0].firstChild.data;
-
txt += ', ';
-
txt += book.getElementsByTagName('condition')[0].firstChild.data;
-
txt += ', ';
-
txt += book.getElementsByTagName('price')[0].firstChild.data;
-
-
txt += '</p>\n';
-
}
-
displayDiv.innerHTML = txt;// + anothertxt;
-
}
9 2741
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: - function getElemText(elem, tag)
-
{
-
var arr, retVal="";
-
-
if( (arr=elem.getElementsByTagName(tag) ) && arr[0] && arr[0].firstChild!='undefined' )
-
retVal=arr[0].firstChild.data;
-
-
return retVal;
-
}
-
-
/* Call like this */
-
-
txt += getElemText(book, 'publisher');
-
-
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.
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.
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.
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?
- <script type="text/javascript" src="xhrlib.js"></script>
-
<script type="text/javascript">
-
-
var displayDiv;
-
var books;
-
var CDs;
-
-
function init(displayDivID)
-
{ displayDiv = document.getElementById(displayDivID);
-
requestXML('data.xml', setup,true);
-
//doBookDisplay();
-
}
-
-
function setup(xmlDoc)
-
{
-
books = xmlDoc.getElementsByTagName('book');
-
CDs = xmlDoc.getElementsByTagName('cd');
-
}
-
-
-
function doBookDisplay()
-
{
-
var txt = '';
-
displayDiv.innerHTML = '';
-
for (var aBook = 0; aBook < books.length; aBook ++)
-
{ var book = books[aBook];
-
-
txt += '<p>';
-
-
if(book.getElementsByTagName('title')){
-
txt += book.getElementsByTagName('title')[0].firstChild.data;
-
}
-
-
txt += ', ';
-
-
if(book.getElementsByTagName('author')){
-
txt += book.getElementsByTagName('author')[0].firstChild.data;
-
txt += ', ';
-
}
-
if(book.getElementsByTagName('publisher')){
-
//txt += book.getElementsByTagName('publisher')[0].firstChild.data;
-
//txt += "working--------------";
-
txt += ', ';
-
//this does not work- no idea why
-
}
-
-
-
if(book.getElementsByTagName('year')){
-
//txt += book.getElementsByTagName('year')[0].firstChild.data;
-
txt += ', ';
-
//this does not work- no idea why
-
}
-
-
if(book.getElementsByTagName('condition')){
-
txt += book.getElementsByTagName('condition')[0].firstChild.data;
-
txt += ', ';
-
}
-
-
-
-
if(book.getElementsByTagName('price')){
-
txt += book.getElementsByTagName('price')[0].firstChild.data;
-
}
-
-
txt += '</p>\n';
-
-
}
-
displayDiv.innerHTML = txt;// + anothertxt;
-
}
-
-
-
-
function doCDDisplay(xmlDoc)
-
{
-
var txt = '';
-
displayDiv.innerHTML = 'cv';
-
for (var aCD = 0; aCD < CDs.length; aCD ++)
-
{ var cd = CDs[aCD];
-
-
txt += 'lkjlkj<p>';
-
-
if(cd.getElementsByTagName('title')){
-
//txt += cd.getElementsByTagName('title')[0].firstChild.data;
-
}
-
-
txt += ', ';
-
-
if(cd.getElementsByTagName('artist')){
-
//txt += cd.getElementsByTagName('artist')[0].firstChild.data;
-
txt += ', ';
-
}
-
if(book.getElementsByTagName('condition')){
-
//txt += book.getElementsByTagName('condition')[0].firstChild.data;
-
txt += ', ';
-
}
-
if(cd.getElementsByTagName('year')){
-
//txt += cd.getElementsByTagName('year')[0].firstChild.data;
-
txt += ', ';
-
//this does not work- no idea why
-
}
-
-
if(cd.getElementsByTagName('price')){
-
//txt += cd.getElementsByTagName('price')[0].firstChild.data;
-
}
-
-
txt += '</p>\n';
-
-
}
-
displayDiv.innerHTML = txt;// + anothertxt;
-
}
-
function here(){
-
displayDiv.innerHTML = 'cleared';
-
}
-
-
</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.
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?
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 - if( getElementsByTagName('xx') )
does not return false if there are no such tags. -
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
-
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
-
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
-
<head>
-
<meta http-equiv="Content-Type"
-
content="text/html; charset=ISO-8859-1"/>
-
<title>Example 16-1 - (Taken from Peter Coxshead's Example Material and edited for Homework) </title>
-
<script type="text/javascript" src="xhrlib.js"></script>
-
<script type="text/javascript">
-
-
function getElemText(elem, tag)
-
{
-
var arr, retVal="";
-
-
if( (arr=elem.getElementsByTagName(tag) ) && arr[0] && arr[0].firstChild!='undefined' )
-
retVal=arr[0].firstChild.data;
-
-
return retVal;
-
}
-
-
var displayDiv;
-
var books;
-
var CDs;
-
-
function init(displayDivID)
-
{ displayDiv = document.getElementById(displayDivID);
-
requestXML('data.xml', setup,true);
-
-
}
-
-
function setup(xmlDoc)
-
{
-
books = xmlDoc.getElementsByTagName('book');
-
CDs = xmlDoc.getElementsByTagName('cd');
-
}
-
-
-
function doBookDisplay()
-
{
-
var txt = '', t;
-
displayDiv.innerHTML = '';
-
-
-
for (var aBook = 0; aBook < books.length; aBook ++)
-
{
-
var book = books[aBook];
-
-
txt += '<p>';
-
-
-
txt += getElemText(book, 'title');
-
txt += ', ';
-
txt += getElemText(book, 'author');
-
txt += ', ';
-
-
if( (t=getElemText(book, 'publisher'))!='')
-
{
-
txt += t + "working--------------"
-
txt += ', ';
-
}
-
-
if( (t=getElemText(book, 'year'))!='')
-
{
-
txt += t;
-
txt += ', ';
-
}
-
-
if( (t=getElemText(book, 'condition'))!='')
-
{
-
txt += t;
-
txt += ', ';
-
}
-
-
t+=getElemText(book, 'price');
-
txt += '</p>\n';
-
-
}
-
displayDiv.innerHTML = txt;// + anothertxt;
-
}
-
-
-
-
function doCDDisplay(xmlDoc)
-
{
-
var txt = '', t;
-
-
displayDiv.innerHTML = 'cv';
-
-
for (var aCD = 0; aCD < CDs.length; aCD++)
-
{
-
var cd = CDs[aCD];
-
-
txt += '<p>';
-
-
if((t=getElemText(cd, 'condition'))!='')
-
{
-
txt+=t;
-
txt += ', ';
-
}
-
-
if((t=getElemText(cd, 'artist'))!='')
-
{
-
txt+=t;
-
txt += ', ';
-
}
-
-
if((t=getElemText(cd, 'year'))!='')
-
{
-
txt+=t;
-
txt += ', ';
-
}
-
-
if((t=getElemText(cd, 'price'))!='')
-
{
-
txt+=t;
-
txt += ', ';
-
}
-
-
txt += '</p>\n';
-
-
}
-
-
displayDiv.innerHTML = txt;
-
}
-
-
function here()
-
{
-
displayDiv.innerHTML = 'cleared';
-
}
-
-
</script>
-
</head>
-
-
<body onload="init('display');">
-
<h1>Books and Books</h1>
-
<p>The data below was obtained from <a href="data.xml">data.xml</a> on the same web server as this page.</p>
-
-
<input type="button" value="Books" onclick="doBookDisplay()" />
-
<input type="button" value="CDs" onclick="doCDDisplay()" />
-
-
<div id="display" onload="doBookDisplay();"></div>
-
-
<input type="button" value="Next" onclick="step('i01')" />
-
</body>
-
</html>
-
-
I'll let you decide the re-write fee.
:p thanks that works great
Sign in to post your reply or Sign up for a free account.
Similar topics |
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>:
...
|
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...
|
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...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |