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 13467
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 = xnl;
XmlNodeType nodeType = xn.NodeType;
|
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 the original name plus a
number(counter).
The problem is I have i have a div 'serials' inside 'readroot' and the
childnodes of
|
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>
<xmlnames>
<field>id</field>
|
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 id="a">a</div>
<div id="b">b</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 have any suggestions?
Thanks.
This sample code works:
| |
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 childNodes. It seems that Firefox does not recognize this line of code >> xmlDoc.getElementsByTagName("urlValue").childNodes.nodeValue
I'm not sure if this is the line that is not working, or the lines before it. I'm looping to the childNodes to compare to...
|
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 TRAN_DESC_CD="ACRT" TRAN_DESC="Actual Rate">
|
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'
attr2='somevalue' attrN='...'>markup</span>"
3 Dim objSpan As HTMLSpanElement =
browser.Document.createElement("span")
4 objSpan.innerHTML = strMarkup
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |