Enjoy an ad free experience by logging in. Not a member yet?
Register .
12-12-2008, 02:19 PM
PM User |
#1
New Coder
Join Date: Dec 2007
Posts: 48
Thanks: 5
Thanked 0 Times in 0 Posts
Nesting of Elements?
Just doing up a simple xml project and was wondering if my code allows for nesting of elements and some element attributes?
wiicat.xml
Code:
<CATALOG>
<CD>
<TITLE>The Golden Compass</TITLE>
<ARTIST>SEGA</ARTIST>
<COUNTRY>PAL</COUNTRY>
<COMPANY>Sega</COMPANY>
<PRICE>55</PRICE>
<YEAR>2008</YEAR>
</CD>
<CD>
<TITLE>Super Mario Galaxy</TITLE>
<ARTIST>Wii</ARTIST>
<COUNTRY>PAL</COUNTRY>
<COMPANY>Nintendo</COMPANY>
<PRICE>45</PRICE>
<YEAR>2007</YEAR>
</CD>
</CATALOG>
xmltest.html
Code:
<html>
<head>
<script type="text/javascript">
var xmlDoc;
if (window.ActiveXObject)
{// code for IE
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
else if (document.implementation.createDocument)
{// code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load("file:///C:/Users/Mike/Desktop/wiicat.xml");
var x=xmlDoc.getElementsByTagName("CD");
function show(i)
{
artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
title=(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
year=(x[i].getElementsByTagName("YEAR")[0].childNodes[0].nodeValue);
country=(x[i].getElementsByTagName("COUNTRY")[0].childNodes[0].nodeValue);
company=(x[i].getElementsByTagName("COMPANY")[0].childNodes[0].nodeValue);
price=(x[i].getElementsByTagName("PRICE")[0].childNodes[0].nodeValue);
txt="Artist: "+artist+"<br />Title: "+title+"<br />Year: "+year+"<br />Country: "+country+"<br />Company: "+company+"<br />Price: "+price ;
document.getElementById("show").innerHTML=txt;
}
</script>
<style type="text/css">
<!--
.style1 {
font-family: "Times New Roman", Times, serif;
font-size: 16px;
color: #003399;
}
body,td,th {
color: #000000;
}
body {
background-color: #FFFFFF;
background-image: url(wii.jpg);
background-repeat: no-repeat;
}
.style3 {
color: #000000;
font-size: xx-large;
}
-->
</style>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
<body>
<div align="justify"></div>
<div class="style1" id='show'>
<div align="center" class="style3">CLICK ON A ROW TO DISPLAY GAME INFO</div>
</div>
<div align="center"><br />
</div>
<p align="center" class="style1">
<script type="text/javascript">
document.write("<table border='1'>");
for (var i=0;i<x.length;i++)
{
document.write("<tr onclick='show(" + i + ")'>");
document.write("<td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
</script>
</p>
</body>
</html>
12-17-2008, 12:02 PM
PM User |
#2
Regular Coder
Join Date: Oct 2006
Location: Ames, Iowa, USA
Posts: 116
Thanks: 9
Thanked 4 Times in 4 Posts
Are you asking whether the JavaScript is valid, or whether you can access nested elements using the JavaScript, or if the XML is valid, or what? Some clarification would be nice.
__________________
-- </byuhobbes>
Users who have thanked byuhobbes85 for this post:
12-17-2008, 02:43 PM
PM User |
#3
New Coder
Join Date: Dec 2007
Posts: 48
Thanks: 5
Thanked 0 Times in 0 Posts
Its in relation to the XML
The code is for a simple xml example but I need to turn it into an XML Schema that allows for nesting of elements and some element attributes
Last edited by mikebyrne; 12-17-2008 at 02:47 PM ..
12-17-2008, 11:53 PM
PM User |
#4
Regular Coder
Join Date: Oct 2006
Location: Ames, Iowa, USA
Posts: 116
Thanks: 9
Thanked 4 Times in 4 Posts
XML represents data in a tree structure. There must be one "root" element (in your case the catalog tag) in which all other elements are contained. After that, you can nest your items as deeply as you would like. Each child of the root is a branch, and each child of that branch is a branch, etc., until you reach an element with no children, which is referred to as a leaf element.
So if I understand correctly, you're asking whether the JavaScript will work if you change the format of the XML (add nested elements and attributes). The answer is...I don't know! It depends on the nature of those changes. Again, an example and/or some clarification would be nice.
__________________
-- </byuhobbes>
12-18-2008, 12:06 AM
PM User |
#5
New Coder
Join Date: Dec 2007
Posts: 48
Thanks: 5
Thanked 0 Times in 0 Posts
Thanks for the reply.
No, my question is how would I change the XML to add the nested elements and attributes.
I'm just wondering how I can amend my code?
12-18-2008, 12:17 AM
PM User |
#6
Regular Coder
Join Date: Oct 2006
Location: Ames, Iowa, USA
Posts: 116
Thanks: 9
Thanked 4 Times in 4 Posts
Quote:
No, my question is how would I change the XML to add the nested elements and attributes.
This is where I could use some clarification. What information do you want to add? Which attributes? How do you want to nest the elements? XML is very flexible and can do pretty much what you want it to do. Some XML structures make more sense and make it easier to access the data, but you can pretty much do what you want.
Quote:
I'm just wondering how I can amend my code?
The changes you make to your code depend on the changes you make to your structure. The code knows nothing about the format of the XML it is receiving--you're the one that determined its structure! You have to tell it what tags to look for, what those tags contain, etc.
__________________
-- </byuhobbes>
12-18-2008, 12:56 AM
PM User |
#7
New Coder
Join Date: Dec 2007
Posts: 48
Thanks: 5
Thanked 0 Times in 0 Posts
My simple xml code refers to a "Catalog" of "Wii" (it used to be cd but ive change the code since posting) games. What I want to do is turn the XML doc into an XML Schema with nested elements and attributes.
I'm just looking for a simple example in relation to my above code
12-18-2008, 05:25 PM
PM User |
#8
New Coder
Join Date: Dec 2007
Posts: 48
Thanks: 5
Thanked 0 Times in 0 Posts
My XML looks like this im just trying to turn it into an XML Schema with nested elements and attributes.
I just looking for examples
Code:
<CATALOG>
<Wii>
<TITLE>The Golden Compass</TITLE>
<ARTIST>SEGA</ARTIST>
<COUNTRY>PAL</COUNTRY>
<PRICE>55</PRICE>
<YEAR>2008</YEAR>
</Wii>
<Wii>
<TITLE>Super Mario Galaxy</TITLE>
<ARTIST>Nintendo</ARTIST>
<COUNTRY>PAL</COUNTRY>
<PRICE>45</PRICE>
<YEAR>2007</YEAR>
</Wii>
<Wii>
<TITLE>Guitar Hero World Tour</TITLE>
<ARTIST>Activision</ARTIST>
<COUNTRY>NTSC</COUNTRY>
<PRICE>75</PRICE>
<YEAR>2007</YEAR>
</Wii>
<Wii>
<TITLE>Need for Speed: Undercover</TITLE>
<ARTIST>EA</ARTIST>
<COUNTRY>PAL</COUNTRY>
<PRICE>55</PRICE>
<YEAR>2008</YEAR>
</Wii>
<Wii>
<TITLE>Quantum of Solace</TITLE>
<ARTIST>Nintendo</ARTIST>
<COUNTRY>PAL</COUNTRY>
<PRICE>50</PRICE>
<YEAR>2008</YEAR>
</Wii>
<Wii>
<TITLE>Soulcalibur Legends</TITLE>
<ARTIST>SEGA</ARTIST>
<COUNTRY>PAL</COUNTRY>
<PRICE>55</PRICE>
<YEAR>2008</YEAR>
</Wii>
<Wii>
<TITLE>Tomb Raider: Anniversary</TITLE>
<ARTIST>EA</ARTIST>
<COUNTRY>PAL</COUNTRY>
<PRICE>65</PRICE>
<YEAR>2007</YEAR>
</Wii>
</CATALOG>
Jump To Top of Thread
Thread Tools
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
HTML code is Off
All times are GMT +1. The time now is 04:37 AM .
Advertisement
Log in to turn off these ads.