JavaScript Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsWeb DesignJavaScript Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old May 11th, 2011, 11:52 AM
dlace dlace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 5 dlace User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 12 m 27 sec
Reputation Power: 0
Please help - Dom Parsing XML file to HTML?

DOM Parsing XML file (am new to using DOM and parsing files)

Hi I am practicing with example files I found online (W3C) and would really appreciate some help with this.

I am trying to use DOM to parse and XML file and then display the info retrieved from the XML file in HTML using Javascript

The files are working well and validate, but nothing is displayed when I open the file up in a browser only the style sheet background color I am using

There are 3 JS functions, one loads the XML, the second gets the info and the third displays it

Here is the code, minus the Style sheet I have been trying to figure this out for days now, is it something little that I have missed? please help thanks

HTML Code

Code:
<?xml version = "1.0" encoding = "utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<head>
<title>Bookstore</title>

<script type = "text/javascript">
function loadXMLDoc(books.xml)
{
var xmlhttp;
if (window.XMLHttpRequest)
  { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
xmlhttp.open("GET",books.xml,false);
xmlhttp.send();
return xmlhttp.responseXML;
  }
 else if (ActiveXObject("Microsoft.XMLDOM")) 
   { 
       xmlhttp = CreateObject( "Microsoft.XMLDOM" )
       xmlhttp.async="false" 
       xmlhttp.load(books.xml) 
       return xmlhttp.responseXML
   } 
alert("Error loading document"); 
   return null; 
}

xmlDoc = loadXMLDoc("books.xml");
var x=xmlDoc.getElementsByTagName("bookstore");
var i=0;

function info(i)
{
title=(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
author=(x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue);

txt="title"<br />"author"<br />"year"<br />";
document.getElementById("showBooks").innerHTML=txt;
}

function display()
{
document.write("<table border='1'; >");
for (var i=0;i<x.length;i++)
  { 
document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("year")[0].childNodes[0].nodeValue );
document.write("</td><td>");
	}
}
 </script>
 </head>
<body onload ="display()">
<div id='showRecipe'></div>
</body>
</html>


Here is the XML File Data

Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>

<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>


Please help thanks really want to see why it won't display

Reply With Quote
  #2  
Old May 11th, 2011, 03:25 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,717 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 7 h 29 m 55 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Moved from XML.


Most of the problems have to do with the JavaScript itself, not the parts of it that deal with the XML.
  • In loadXMLDoc(), the stuff with "books.xml" is very invalid. If you want it as a string, use quotes. If you want it as an argument, use a valid variable name.
  • In info(), the line where you assign txt= is also very invalid. You can't mix variables and strings that way - use the + operator (or some other method).
  • In display(), the <table> tag you try to write shouldn't have that semicolon.

As for the XML-related mistake,
  • x is a list of <bookstore> nodes. That's the root element so there's only one of them. You should be looking at the <book>s.

Reply With Quote
  #3  
Old May 11th, 2011, 05:04 PM
dlace dlace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 5 dlace User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 12 m 27 sec
Reputation Power: 0
Dom Parsing XML file to HTML?

Thank you for your help I really appreciate it, and I apologise for posting in the wrong place. I'm just a Noob, I will go over what you have provided and will let you know how it goes.....

Reply With Quote
  #4  
Old May 11th, 2011, 05:17 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,717 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 7 h 29 m 55 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
If you knew that it was a JavaScript error then yeah, it belonged here in the JavaScript forum.
But you didn't (or so it seems). You thought it was an XML thing. Or at least something to do with the XML code bits.
So you posting it in XML was correct. So you don't have to apologize for that.
Comments on this post
dlace agrees: thanks for your help

Reply With Quote
  #5  
Old May 13th, 2011, 07:29 PM
dlace dlace is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2010
Posts: 5 dlace User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 12 m 27 sec
Reputation Power: 0
Thanks for your help

hi requinix,

thanks for all your help I managed to figure out the probs based on what you told me too look at and it is now working fine awesome

Reply With Quote
Reply

Viewing: Dev Shed ForumsWeb DesignJavaScript Development > Dom Parsing XML file to HTML?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap