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 2721
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: Rina0 |
last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: erikbower65 |
last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA:
1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
|
by: Taofi |
last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same
This are my field names
ID, Budgeted, Actual, Status and Differences
...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
| |