Here's the latest snag in the code:
I was able to retrieve data from the most of the xml files except for the xml files that contain attributes but I have yet to find a tutorial that explains the retrieval of the attributes of a node.
Here's the XML: -
-
<episode>
-
<title>Episode 101 - DECEMBER 5TH, 2006</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Heads Will Roll by MARION RAVEN" songurl="http://www.marion-raven.com/"></music>
-
<music songname="All Right by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
-
<music songname="OK, OK by SLUNT" songurl="http://www.myspace.com/slunt"> </music>
-
<music songname="Here I Come by FERGIE" songurl="http://www.fergie.blackeyedpeas.com/"> </music>
-
<music songname="Shoot From The Hip by A CHANGE OF PACE" songurl="http://www.achangeofpacemusic.com/"> </music>
-
<music songname="Reputation by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
-
<music songname="Love Is All That Matters by SARA HAZE" songurl="http://www.myspace.com/sarahaze"> </music>
-
<music songname="TV Land by SUPERCHICK" songurl="http://www.superchickonline.com/"> </music>
-
</episode>
-
-
Here's the javascript : -
-
var xmlObj=xmlDoc.documentElement;
-
var titlevar=xmlDoc.getElementsByTagName('title');
-
var musicvar=xmlDoc.getElementsByTagName('music');
-
-
for (i=0;i<titlevar.length;i++)
-
{
-
if (titlevar[i].nodeType==1)
-
{
-
document.writeln(titlevar[i].childNodes[0].nodeValue);
-
document.writeln("<br>");
-
//document.writeln(musicvar[i].childNodes[0].nodeValue);
-
document.writeln(xmlObj.childNodes[1].getAttribute("songname"));
-
document.writeln(xmlObj.childNodes[1].getAttribute("songurl"));
-
document.writeln("<br>");
-
}
-
}
-
-
Here's what it returns: -
-
Episode 101 - DECEMBER 5TH, 2006
-
null null
-
Episode 102 - DECEMBER 12TH, 2006
-
null null
-
Episode 103 - DECEMBER 19TH, 2006
-
null null
-
Episode 104 - DECEMBER 26TH, 2006
-
null null
-
Episode 105 - JANUARY 9TH, 2007
-
null null
-
Episode 106 - JANUARY 16TH, 2007
-
null null
-
Episode 107 - JANUARY 23RD, 2007
-
null null
-
Episode 108 - JANUARY 30TH, 2007
-
null null
-
Episode 109 - FEBRUARY 6TH, 2007
-
null null
-
-
7 13443
Welcome to TheScripts TSDN....
In the for loop, try this: -
for (i=0;i<musicvar.length;i++){
-
document.writeln(musicvar[i].getAttribute('songname'));
-
document.writeln(musicvar[i].getAttribute('songurl'));
-
}
Or you can use XPath with selectNodes() method.
See this link, and related chapters there.
As with the last time, I should have searched more before posting. I am able to get the Attributes this way: -
-
musics = xml_doc.getElementsByTagName("music");
-
titles = xml_doc.getElementsByTagName("title");
-
n_musics = musics.length;
-
-
for (j = 0; j < titles.length; j++)
-
{
-
document.writeln(titles[j].childNodes[0].nodeValue + "<br>");
-
myMusicAttribFunction();
-
}
-
-
function myMusicAttribFunction ()
-
{
-
for(i = 0; i < musics.length; i++)
-
{
-
Musicvar = musics.item(i);
-
attr1 = Musicvar.getAttribute("songname");
-
attr2 = Musicvar.getAttribute("songurl");
-
document.write("<a href='" + attr2 + "' target='_blank'>" + attr1 + "</a><br>");
-
}
-
}
-
-
The only problem is that it's returning the "title" and then it proceeds to retrieve every songname and songurl, then returning another "title" and giving me the entire XML songname and songurl again.
Here's the output, what am I missing? -
-
Episode 101 - DECEMBER 5TH, 2006
-
Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
-
Heads Will Roll by MARION RAVEN
-
All Right by THE ETTES
-
OK, OK by SLUNT
-
Here I Come by FERGIE
-
Shoot From The Hip by A CHANGE OF PACE
-
Reputation by THE ETTES
-
Love Is All That Matters by SARA HAZE
-
TV Land by SUPERCHICK
-
Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
-
No Way Back by 8MM
-
Untold by SHAAN SHARMA
-
Closely Sadly by TRENTALANGE
-
Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
-
Nice To Know You by VIA AUDIO
-
Better With You by FIVE TIMES AUGUST
-
Gimme by THE ETTES
-
Hot Machette (Get Ready) by Jonezetta
-
Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
-
Let A Thug Hit It by BABY DOWN
-
School Bitches by 1800 BLK
-
Untitled by INGRID MICHAELSON
-
So Alive by LEEDSY PORTS
-
Messin Around by ALANA D
-
Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
-
Bad For Me by THE SLEEPERS
-
Shine Like A Star by LEEDSY PORTS
-
Die Alone by INGRID MICHAELSON
-
Does She Realize? by HOLD FOR SWANK
-
Where The Glory Dwells by CHARITY DAW
-
Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
-
Mr Melrose Ave by CASHMERE JUNGLE LORDS
-
The Way I Am by INGRID MICHAELSON
-
Die Alone by INGRID MICHAELSON
-
Hypnotized by PITCH BLACK DREAM
-
Hollywood by DENA DEADLY
-
Cliche Rock n Roll Party Song by SLUNT
-
Get Free by DENA DEADLY
-
Head Games by DENA DEADLY
-
Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
-
Dirty by THE ETTES
-
etc...
-
I understand.
Let me try a test, using XPath, and I'll come back with the result.
I succeeded without XPath: - titles = xml_doc.getElementsByTagName("title");
-
-
for (j = 0; j < titles.length; j++)
-
{
-
document.writeln(titles[j].childNodes[0].nodeValue + "<br>");
-
myMusicAttribFunction(titles[j].parentNode);
-
}
-
function myMusicAttribFunction (node)
-
{
-
-
for(i = 1; i < node.childNodes.length; i++)
-
{
-
Musicvar = node.childNodes[i];
-
attr1 = Musicvar.getAttribute("songname");
-
attr2 = Musicvar.getAttribute("songurl");
-
document.write("<a href='" + attr2 + "' target='_blank'>" + attr1 + "</a><br>");
-
}
-
}
-
Hey Doron. Thanks for the help but it returned null, so I'm assumming that this has to do with me not giving you all the xml code. Here it is: -
-
<?xml version="1.0" encoding="UTF-8"?>
-
<BGmusic>
-
<episode>
-
<title>Episode 101 - DECEMBER 5TH, 2006</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Heads Will Roll by MARION RAVEN" songurl="http://www.marion-raven.com/"></music>
-
<music songname="All Right by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
-
<music songname="OK, OK by SLUNT" songurl="http://www.myspace.com/slunt"> </music>
-
<music songname="Here I Come by FERGIE" songurl="http://www.fergie.blackeyedpeas.com/"> </music>
-
<music songname="Shoot From The Hip by A CHANGE OF PACE" songurl="http://www.achangeofpacemusic.com/"> </music>
-
<music songname="Reputation by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
-
<music songname="Love Is All That Matters by SARA HAZE" songurl="http://www.myspace.com/sarahaze"> </music>
-
<music songname="TV Land by SUPERCHICK" songurl="http://www.superchickonline.com/"> </music>
-
</episode>
-
<episode>
-
<title>Episode 102 - DECEMBER 12TH, 2006</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="No Way Back by 8MM" songurl="http://www.8mmaudio.com/"></music>
-
<music songname="Untold by SHAAN SHARMA" songurl="http://www.shaansharma.com/"> </music>
-
<music songname="Closely Sadly by TRENTALANGE" songurl="http://www.trentalangemusic.com/"> </music>
-
</episode>
-
<episode>
-
<title>Episode 103 - DECEMBER 19TH, 2006</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Nice To Know You by VIA AUDIO" songurl="http://www.viaaudio.net/"></music>
-
<music songname="Better With You by FIVE TIMES AUGUST" songurl="http://www.fivetimesaugust.com/"> </music>
-
<music songname="Gimme by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
-
<music songname="Hot Machette (Get Ready) by Jonezetta" songurl="http://www.jonezetta.com/"></music>
-
</episode>
-
<episode>
-
<title>Episode 104 - DECEMBER 26TH, 2006</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Let A Thug Hit It by BABY DOWN" songurl="http://www.myspace.com/babydown"></music>
-
<music songname="School Bitches by 1800 BLK" songurl="http://1800blkentertainment.com/beta/main.html"> </music>
-
<music songname="Untitled by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
-
<music songname="So Alive by LEEDSY PORTS" songurl="http://www.i94fans.com/leedsyports.html"></music>
-
<music songname="Messin Around by ALANA D"></music>
-
</episode>
-
<episode>
-
<title>Episode 105 - JANUARY 9TH, 2007</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Bad For Me by THE SLEEPERS" songurl="http://www.thesleepersonline.com/index.cfm"></music>
-
<music songname="Shine Like A Star by LEEDSY PORTS" songurl="http://www.i94fans.com/leedsyports.html"> </music>
-
<music songname="Die Alone by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
-
<music songname="Does She Realize? by HOLD FOR SWANK" songurl="http://www.holdforswank.com"></music>
-
<music songname="Where The Glory Dwells by CHARITY DAW" songurl="http://www.charitydaw.com"></music>
-
</episode>
-
<episode>
-
<title>Episode 106 - JANUARY 16TH, 2007</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Mr Melrose Ave by CASHMERE JUNGLE LORDS" songurl="http://www.cashmerejunglelords.com"></music>
-
<music songname="The Way I Am by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
-
<music songname="Die Alone by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
-
<music songname="Hypnotized by PITCH BLACK DREAM" songurl="http://www.pitchblackdream.com"></music>
-
<music songname="Hollywood by DENA DEADLY" songurl="http://www.myspace.com/8586297"></music>
-
<music songname="Cliche Rock n Roll Party Song by SLUNT" songurl="http://www.slunt.net"></music>
-
<music songname="Get Free by DENA DEADLY" songurl="http://www.myspace.com/8586297"></music>
-
<music songname="Head Games by DENA DEADLY" songurl="http://www.myspace.com/8586297"></music>
-
</episode>
-
<episode>
-
<title>Episode 107 - JANUARY 23RD, 2007</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Dirty by THE ETTES" songurl="http://www.theettes.com"></music>
-
<music songname="Moon Thoughts by RHYTHM D" songurl="http://www.myspace.com/rhythmd"> </music>
-
<music songname="Death Wish by MARK MALLMAN" songurl="http://www.mallman.com"> </music>
-
<music songname="Damn Its Incredible by RHYTHM D" songurl="http://www.myspace.com/rhythmd"></music>
-
<music songname="Riley by MEGAN SLANKARD" songurl="http://www.meganslankard.com"></music>
-
<music songname="Get It Wrong by THE CUTTERS" songurl="http://www.blackjackhumboldt.com/cutters.html"></music>
-
<music songname="BD Exclusive by RHYTHM D" songurl="http://www.myspace.com/rhythmd"></music>
-
</episode>
-
<episode>
-
<title>Episode 108 - JANUARY 30TH, 2007</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="I Wanna Go Home by SAMANTHA MURPHY" songurl="http://www.samanthamurphy.com/"></music>
-
<music songname="Big Rock For U by HOLD FOR SWANK" songurl="http://www.holdforswank.com/"> </music>
-
<music songname="Sleeping With The Enemy by HOLD FOR SWANK" songurl="http://www.holdforswank.com/"> </music>
-
<music songname="Loosen Up Man by HOLD FOR SWANK" songurl="http://www.holdforswank.com/"></music>
-
</episode>
-
<episode>
-
<title>Episode 109 - FEBRUARY 6TH, 2007</title>
-
<music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Almost Perfect by VAUGHAN PENN" songurl="http://www.vaughanpenn.com/intro.htm"></music>
-
<music songname="Party of the MOB by THA REALEST" songurl="http://www.myspace.com/tharealestomerta"> </music>
-
<music songname="Back on the Beach by I-94" songurl="http://www.i94fans.com/leedsyports.html"> </music>
-
<music songname="A Little Sunshine by I-94" songurl="http://www.i94fans.com/leedsyports.html"></music>
-
<music songname="Unraveled by SUSAN HOWARD" songurl="http://www.myspace.com/susanhoward"></music>
-
<music songname="One Step Closer by VAUGHAN PENN" songurl="http://www.vaughanpenn.com/intro.htm"></music>
-
</episode>
-
</BGmusic>
-
Any help is very much appreciated :-)
OK! It's a hack, but I figure it out. I had to add an attribute into the XML for the first node "music" of each episode called "id" and I set the "id" to "newepisode"
Snippet of XML file: -
-
<BGmusic>
-
<episode>
-
<title>Episode 101 - DECEMBER 5TH, 2006</title>
-
<music id="newepisode" songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
<music songname="Heads Will Roll by MARION RAVEN" songurl="http://www.marion-raven.com/"></music>
-
<music songname="All Right by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
-
<music songname="OK, OK by SLUNT" songurl="http://www.myspace.com/slunt"> </music>
-
<music songname="Here I Come by FERGIE" songurl="http://www.fergie.blackeyedpeas.com/"> </music>
-
<music songname="Shoot From The Hip by A CHANGE OF PACE" songurl="http://www.achangeofpacemusic.com/"> </music>
-
<music songname="Reputation by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
-
<music songname="Love Is All That Matters by SARA HAZE" songurl="http://www.myspace.com/sarahaze"> </music>
-
<music songname="TV Land by SUPERCHICK" songurl="http://www.superchickonline.com/"> </music>
-
</episode>
-
<episode>
-
<title>Episode 102 - DECEMBER 12TH, 2006</title>
-
<music id="newepisode" songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
-
-
It was easy from that point -- here's the javascript : -
try
-
{
-
if (window.ActiveXObject)
-
{
-
var errorHappendHere = "Check Browser and security settings";
-
xml_doc = new ActiveXObject("Microsoft.XMLDOM");
-
xml_doc.async=false;
-
xml_doc.load('xml/badgirls_music.xml');
-
}
-
else if(window.XMLHttpRequest)
-
{
-
var errorHappendHere = "Error handling XMLHttpRequest request";
-
var d = new XMLHttpRequest();
-
d.open("GET", 'xml/badgirls_music.xml', false);
-
d.send(null);
-
xml_doc=d.responseXML;
-
} else {
-
var errorHappendHere = "Error.";
-
xml_doc = document.implementation.createDocument("","",null);
-
xml_doc.async=false;
-
xml_doc.load('xml/badgirls_music.xml');
-
}
-
}
-
catch(e)
-
{
-
alert(errorHappendHere);
-
}
-
musics = xml_doc.getElementsByTagName("music");
-
titles = xml_doc.getElementsByTagName("title");
-
-
j=0;
-
for(i = 0; i < musics.length; i++)
-
{
-
if (musics[i].nodeType==1)
-
{
-
Musicvar = musics.item(i);
-
attr1 = Musicvar.getAttribute("songname");
-
attr2 = Musicvar.getAttribute("songurl");
-
attr3 = Musicvar.getAttribute("id");
-
if(attr3 == "newepisode")
-
{
-
document.writeln(titles[j].childNodes[0].nodeValue + "<br>");
-
j++;
-
}
-
document.write("<a href='" + attr2 + "' target='_blank'>" + attr1 + "</a><br>");
-
}
-
}
-
Thanks for your input Doron, you got me thinking in the right direction. :-)
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Hans Kamp |
last post by:
My program fails reading XML attributes. A fragment of the code is:
private void showXmlNodeAtTreeNode(XmlNodeList xnl, TreeNode tn)
{
int i;
for (i = 0; i < xnl.Count; i++)
{
XmlNode xn =...
|
by: chuck |
last post by:
Hi,
I am modifying some code from here
http://www.quirksmode.org/dom/domform.html
I have a div 'readroot' that I clone. I change the change the id and
name of the childnodes of 'readroot' to...
|
by: Q1tum |
last post by:
Hi all,
I have a problem with getting the amount of childs in a XML structure,
the strucure is somewhat like the following:
<?xml version="1.0" encoding="iso-8859-1"?>
<cms>
<num>21</num>...
|
by: yawnmoth |
last post by:
Given an element ID, is there a way to figure out what index one would
need to use in the parentNode's childNodes array to get at that
element?
For example...
<body>
<div id="parent">
<div...
|
by: howard15 |
last post by:
Hi,
I'm having a problem with Javascript reading childnodes. For some weird reason, javascript will not read the childnodes correctly if I have the form fields in separate <td> tags.
Anyone...
|
by: Moses |
last post by:
HI
The Value for childNodes.length differs with mozilla and IE
Is it problem with my coding..... I could not under
stood.............
The following is the details
|
by: willyWEB66 |
last post by:
Hi everyone,
I have this code in javascript where the XML file is loaded and displayed to an html using XSLT. It works fine in IE but not in Firefox. My problem is in the looping to the...
|
by: rds80 |
last post by:
In the xml document below, I would like to retrieve the distinct
attributes for the element '<Bal>'. However, I haven't had any
success. Here is what I have so far:
<TRANS>
<TRAN...
|
by: cloftis |
last post by:
Using VS2003, VB and MSHTML,
Using an HTMLSpanElement I want to enumerate the attributes of a SPAN
tag.
1 'For testing sake
2 Dim strMarkup as String = "<span attr1='somevalue'...
|
by: DolphinDB |
last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation.
Take...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |