...

View Full Version : Nesting of Elements?



mikebyrne
12-12-2008, 03:19 PM
Just doing up a simple xml project and was wondering if my code allows for nesting of elements and some element attributes?

wiicat.xml



<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


<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>

byuhobbes85
12-17-2008, 01:02 PM
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.

mikebyrne
12-17-2008, 03:43 PM
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

byuhobbes85
12-18-2008, 12:53 AM
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.

mikebyrne
12-18-2008, 01:06 AM
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?

byuhobbes85
12-18-2008, 01:17 AM
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.


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.

mikebyrne
12-18-2008, 01:56 AM
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

mikebyrne
12-18-2008, 06:25 PM
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



<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum