473,320 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How do I get the attributes of the childnodes xml javascript?

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:

Expand|Select|Wrap|Line Numbers
  1.  
  2.     <episode>
  3.         <title>Episode 101 - DECEMBER 5TH, 2006</title>
  4.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  5.         <music songname="Heads Will Roll by MARION RAVEN" songurl="http://www.marion-raven.com/"></music>
  6.         <music songname="All Right by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
  7.         <music songname="OK, OK by SLUNT" songurl="http://www.myspace.com/slunt"> </music>
  8.         <music songname="Here I Come by FERGIE" songurl="http://www.fergie.blackeyedpeas.com/"> </music>
  9.         <music songname="Shoot From The Hip by A CHANGE OF PACE" songurl="http://www.achangeofpacemusic.com/"> </music>
  10.         <music songname="Reputation by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
  11.         <music songname="Love Is All That Matters by SARA HAZE" songurl="http://www.myspace.com/sarahaze"> </music>
  12.         <music songname="TV Land by SUPERCHICK" songurl="http://www.superchickonline.com/"> </music>
  13.     </episode>
  14.  
  15.  
Here's the javascript:

Expand|Select|Wrap|Line Numbers
  1.  
  2. var xmlObj=xmlDoc.documentElement;
  3. var titlevar=xmlDoc.getElementsByTagName('title');
  4. var musicvar=xmlDoc.getElementsByTagName('music');
  5.  
  6. for (i=0;i<titlevar.length;i++)
  7.   {
  8.   if (titlevar[i].nodeType==1)
  9.     { 
  10. document.writeln(titlevar[i].childNodes[0].nodeValue);
  11. document.writeln("<br>");
  12. //document.writeln(musicvar[i].childNodes[0].nodeValue);
  13. document.writeln(xmlObj.childNodes[1].getAttribute("songname"));
  14. document.writeln(xmlObj.childNodes[1].getAttribute("songurl"));
  15. document.writeln("<br>");
  16.     }
  17.   } 
  18.  
  19.  
Here's what it returns:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Episode 101 - DECEMBER 5TH, 2006 
  3. null null 
  4. Episode 102 - DECEMBER 12TH, 2006 
  5. null null 
  6. Episode 103 - DECEMBER 19TH, 2006 
  7. null null 
  8. Episode 104 - DECEMBER 26TH, 2006 
  9. null null 
  10. Episode 105 - JANUARY 9TH, 2007 
  11. null null 
  12. Episode 106 - JANUARY 16TH, 2007 
  13. null null 
  14. Episode 107 - JANUARY 23RD, 2007 
  15. null null 
  16. Episode 108 - JANUARY 30TH, 2007 
  17. null null 
  18. Episode 109 - FEBRUARY 6TH, 2007 
  19. null null 
  20.  
  21.  
May 16 '07 #1
7 13443
dorinbogdan
839 Expert 512MB
Welcome to TheScripts TSDN....

In the for loop, try this:
Expand|Select|Wrap|Line Numbers
  1. for (i=0;i<musicvar.length;i++){
  2.      document.writeln(musicvar[i].getAttribute('songname'));
  3.      document.writeln(musicvar[i].getAttribute('songurl'));
  4. }
May 16 '07 #2
dorinbogdan
839 Expert 512MB
Or you can use XPath with selectNodes() method.
See this link, and related chapters there.
May 16 '07 #3
As with the last time, I should have searched more before posting. I am able to get the Attributes this way:

Expand|Select|Wrap|Line Numbers
  1.  
  2. musics = xml_doc.getElementsByTagName("music"); 
  3. titles = xml_doc.getElementsByTagName("title");
  4. n_musics = musics.length;
  5.  
  6. for (j = 0; j < titles.length; j++) 
  7.         document.writeln(titles[j].childNodes[0].nodeValue + "<br>");
  8.         myMusicAttribFunction();
  9. }
  10.  
  11. function myMusicAttribFunction ()
  12. {
  13.         for(i = 0; i < musics.length; i++)
  14.         {
  15.            Musicvar = musics.item(i); 
  16.            attr1 = Musicvar.getAttribute("songname");
  17.            attr2 = Musicvar.getAttribute("songurl"); 
  18.            document.write("<a href='" + attr2 + "' target='_blank'>" + attr1 + "</a><br>");
  19.        }
  20. }
  21.  
  22.  
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. Episode 101 - DECEMBER 5TH, 2006
  3. Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
  4. Heads Will Roll by MARION RAVEN
  5. All Right by THE ETTES
  6. OK, OK by SLUNT
  7. Here I Come by FERGIE
  8. Shoot From The Hip by A CHANGE OF PACE
  9. Reputation by THE ETTES
  10. Love Is All That Matters by SARA HAZE
  11. TV Land by SUPERCHICK
  12. Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
  13. No Way Back by 8MM
  14. Untold by SHAAN SHARMA
  15. Closely Sadly by TRENTALANGE
  16. Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
  17. Nice To Know You by VIA AUDIO
  18. Better With You by FIVE TIMES AUGUST
  19. Gimme by THE ETTES
  20. Hot Machette (Get Ready) by Jonezetta
  21. Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
  22. Let A Thug Hit It by BABY DOWN
  23. School Bitches by 1800 BLK
  24. Untitled by INGRID MICHAELSON
  25. So Alive by LEEDSY PORTS
  26. Messin Around by ALANA D
  27. Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
  28. Bad For Me by THE SLEEPERS
  29. Shine Like A Star by LEEDSY PORTS
  30. Die Alone by INGRID MICHAELSON
  31. Does She Realize? by HOLD FOR SWANK
  32. Where The Glory Dwells by CHARITY DAW
  33. Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
  34. Mr Melrose Ave by CASHMERE JUNGLE LORDS
  35. The Way I Am by INGRID MICHAELSON
  36. Die Alone by INGRID MICHAELSON
  37. Hypnotized by PITCH BLACK DREAM
  38. Hollywood by DENA DEADLY
  39. Cliche Rock n Roll Party Song by SLUNT
  40. Get Free by DENA DEADLY
  41. Head Games by DENA DEADLY
  42. Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN
  43. Dirty by THE ETTES
  44. etc...
  45.  
May 16 '07 #4
dorinbogdan
839 Expert 512MB
I understand.
Let me try a test, using XPath, and I'll come back with the result.
May 16 '07 #5
dorinbogdan
839 Expert 512MB
I succeeded without XPath:
Expand|Select|Wrap|Line Numbers
  1.     titles = xml_doc.getElementsByTagName("title");
  2.  
  3.     for (j = 0; j < titles.length; j++)
  4.     {
  5.         document.writeln(titles[j].childNodes[0].nodeValue + "<br>");
  6.         myMusicAttribFunction(titles[j].parentNode);
  7.     }
  8.     function myMusicAttribFunction (node)
  9.     {
  10.  
  11.         for(i = 1; i < node.childNodes.length; i++)
  12.         {
  13.             Musicvar = node.childNodes[i];
  14.             attr1 = Musicvar.getAttribute("songname");
  15.             attr2 = Musicvar.getAttribute("songurl");
  16.             document.write("<a href='" + attr2 + "' target='_blank'>" + attr1 + "</a><br>");
  17.         }
  18.     }            
  19.  
May 16 '07 #6
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <?xml version="1.0" encoding="UTF-8"?>
  3. <BGmusic>
  4.     <episode>
  5.         <title>Episode 101 - DECEMBER 5TH, 2006</title>
  6.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  7.         <music songname="Heads Will Roll by MARION RAVEN" songurl="http://www.marion-raven.com/"></music>
  8.         <music songname="All Right by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
  9.         <music songname="OK, OK by SLUNT" songurl="http://www.myspace.com/slunt"> </music>
  10.         <music songname="Here I Come by FERGIE" songurl="http://www.fergie.blackeyedpeas.com/"> </music>
  11.         <music songname="Shoot From The Hip by A CHANGE OF PACE" songurl="http://www.achangeofpacemusic.com/"> </music>
  12.         <music songname="Reputation by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
  13.         <music songname="Love Is All That Matters by SARA HAZE" songurl="http://www.myspace.com/sarahaze"> </music>
  14.         <music songname="TV Land by SUPERCHICK" songurl="http://www.superchickonline.com/"> </music>
  15.     </episode>
  16.     <episode>
  17.         <title>Episode 102 - DECEMBER 12TH, 2006</title>
  18.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  19.         <music songname="No Way Back by 8MM" songurl="http://www.8mmaudio.com/"></music>
  20.         <music songname="Untold by SHAAN SHARMA" songurl="http://www.shaansharma.com/"> </music>
  21.         <music songname="Closely Sadly by TRENTALANGE" songurl="http://www.trentalangemusic.com/"> </music>
  22.     </episode>
  23.     <episode>
  24.         <title>Episode 103 - DECEMBER 19TH, 2006</title>
  25.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  26.         <music songname="Nice To Know You by VIA AUDIO" songurl="http://www.viaaudio.net/"></music>
  27.         <music songname="Better With You by FIVE TIMES AUGUST" songurl="http://www.fivetimesaugust.com/"> </music>
  28.         <music songname="Gimme by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
  29.         <music songname="Hot Machette (Get Ready) by Jonezetta" songurl="http://www.jonezetta.com/"></music>
  30.     </episode>
  31.     <episode>
  32.         <title>Episode 104 - DECEMBER 26TH, 2006</title>
  33.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  34.         <music songname="Let A Thug Hit It by BABY DOWN" songurl="http://www.myspace.com/babydown"></music>
  35.         <music songname="School Bitches by 1800 BLK" songurl="http://1800blkentertainment.com/beta/main.html"> </music>
  36.         <music songname="Untitled by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
  37.         <music songname="So Alive by LEEDSY PORTS" songurl="http://www.i94fans.com/leedsyports.html"></music>
  38.         <music songname="Messin Around by ALANA D"></music>
  39.     </episode>
  40.     <episode>
  41.         <title>Episode 105 - JANUARY 9TH, 2007</title>
  42.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  43.         <music songname="Bad For Me by THE SLEEPERS" songurl="http://www.thesleepersonline.com/index.cfm"></music>
  44.         <music songname="Shine Like A Star by LEEDSY PORTS" songurl="http://www.i94fans.com/leedsyports.html"> </music>
  45.         <music songname="Die Alone by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
  46.         <music songname="Does She Realize? by HOLD FOR SWANK" songurl="http://www.holdforswank.com"></music>
  47.         <music songname="Where The Glory Dwells by CHARITY DAW" songurl="http://www.charitydaw.com"></music>
  48.     </episode>
  49.     <episode>
  50.         <title>Episode 106 - JANUARY 16TH, 2007</title>
  51.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  52.         <music songname="Mr Melrose Ave by CASHMERE JUNGLE LORDS" songurl="http://www.cashmerejunglelords.com"></music>
  53.         <music songname="The Way I Am by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
  54.         <music songname="Die Alone by INGRID MICHAELSON" songurl="http://www.ingridmichaelson.com"> </music>
  55.         <music songname="Hypnotized by PITCH BLACK DREAM" songurl="http://www.pitchblackdream.com"></music>
  56.         <music songname="Hollywood by DENA DEADLY" songurl="http://www.myspace.com/8586297"></music>
  57.         <music songname="Cliche Rock n Roll Party Song by SLUNT" songurl="http://www.slunt.net"></music>
  58.         <music songname="Get Free by DENA DEADLY" songurl="http://www.myspace.com/8586297"></music>
  59.         <music songname="Head Games by DENA DEADLY" songurl="http://www.myspace.com/8586297"></music>
  60.     </episode>
  61.     <episode>
  62.         <title>Episode 107 - JANUARY 23RD, 2007</title>
  63.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  64.         <music songname="Dirty by THE ETTES" songurl="http://www.theettes.com"></music>
  65.         <music songname="Moon Thoughts by RHYTHM D" songurl="http://www.myspace.com/rhythmd"> </music>
  66.         <music songname="Death Wish by MARK MALLMAN" songurl="http://www.mallman.com"> </music>
  67.         <music songname="Damn Its Incredible by RHYTHM D" songurl="http://www.myspace.com/rhythmd"></music>
  68.         <music songname="Riley by MEGAN SLANKARD" songurl="http://www.meganslankard.com"></music>
  69.         <music songname="Get It Wrong by THE CUTTERS" songurl="http://www.blackjackhumboldt.com/cutters.html"></music>
  70.         <music songname="BD Exclusive by RHYTHM D" songurl="http://www.myspace.com/rhythmd"></music>
  71.     </episode>
  72.     <episode>
  73.         <title>Episode 108 - JANUARY 30TH, 2007</title>
  74.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  75.         <music songname="I Wanna Go Home by SAMANTHA MURPHY" songurl="http://www.samanthamurphy.com/"></music>
  76.         <music songname="Big Rock For U by HOLD FOR SWANK" songurl="http://www.holdforswank.com/"> </music>
  77.         <music songname="Sleeping With The Enemy by HOLD FOR SWANK" songurl="http://www.holdforswank.com/"> </music>
  78.         <music songname="Loosen Up Man by HOLD FOR SWANK" songurl="http://www.holdforswank.com/"></music>
  79.     </episode>
  80.     <episode>
  81.         <title>Episode 109 - FEBRUARY 6TH, 2007</title>
  82.         <music songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  83.         <music songname="Almost Perfect by VAUGHAN PENN" songurl="http://www.vaughanpenn.com/intro.htm"></music>
  84.         <music songname="Party of the MOB by THA REALEST" songurl="http://www.myspace.com/tharealestomerta"> </music>
  85.         <music songname="Back on the Beach by I-94" songurl="http://www.i94fans.com/leedsyports.html"> </music>
  86.         <music songname="A Little Sunshine by I-94" songurl="http://www.i94fans.com/leedsyports.html"></music>
  87.         <music songname="Unraveled by SUSAN HOWARD" songurl="http://www.myspace.com/susanhoward"></music>
  88.         <music songname="One Step Closer by VAUGHAN PENN" songurl="http://www.vaughanpenn.com/intro.htm"></music>
  89.     </episode>
  90. </BGmusic>
  91.  
Any help is very much appreciated :-)
May 16 '07 #7
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. <BGmusic>
  3.     <episode>
  4.         <title>Episode 101 - DECEMBER 5TH, 2006</title>
  5.         <music id="newepisode" songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  6.         <music songname="Heads Will Roll by MARION RAVEN" songurl="http://www.marion-raven.com/"></music>
  7.         <music songname="All Right by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
  8.         <music songname="OK, OK by SLUNT" songurl="http://www.myspace.com/slunt"> </music>
  9.         <music songname="Here I Come by FERGIE" songurl="http://www.fergie.blackeyedpeas.com/"> </music>
  10.         <music songname="Shoot From The Hip by A CHANGE OF PACE" songurl="http://www.achangeofpacemusic.com/"> </music>
  11.         <music songname="Reputation by THE ETTES" songurl="http://www.myspace.com/theettes"> </music>
  12.         <music songname="Love Is All That Matters by SARA HAZE" songurl="http://www.myspace.com/sarahaze"> </music>
  13.         <music songname="TV Land by SUPERCHICK" songurl="http://www.superchickonline.com/"> </music>
  14.     </episode>
  15.     <episode>
  16.         <title>Episode 102 - DECEMBER 12TH, 2006</title>
  17.         <music id="newepisode" songname="Main Title Theme: Love Me Or Hate Me by LADY SOVEREIGN" songurl="http://www.ladysovereign.com"></music>
  18.  
  19.  
It was easy from that point -- here's the javascript:

Expand|Select|Wrap|Line Numbers
  1. try
  2.     {
  3.         if (window.ActiveXObject)
  4.         {
  5.             var errorHappendHere = "Check Browser and security settings";
  6.             xml_doc = new ActiveXObject("Microsoft.XMLDOM");
  7.             xml_doc.async=false;
  8.             xml_doc.load('xml/badgirls_music.xml');
  9.         }
  10.         else if(window.XMLHttpRequest)
  11.         {
  12.             var errorHappendHere = "Error handling XMLHttpRequest request";
  13.             var d = new XMLHttpRequest();
  14.             d.open("GET", 'xml/badgirls_music.xml', false);
  15.             d.send(null);
  16.             xml_doc=d.responseXML;
  17.         } else {
  18.             var errorHappendHere = "Error.";
  19.             xml_doc = document.implementation.createDocument("","",null);
  20.             xml_doc.async=false;
  21.             xml_doc.load('xml/badgirls_music.xml');
  22.         }
  23.     }    
  24.     catch(e)
  25.     {
  26.         alert(errorHappendHere);
  27.     }
  28. musics = xml_doc.getElementsByTagName("music"); 
  29. titles = xml_doc.getElementsByTagName("title");
  30.  
  31.     j=0;
  32.      for(i = 0; i < musics.length; i++)
  33.     {
  34.       if (musics[i].nodeType==1)
  35.         { 
  36.         Musicvar = musics.item(i); 
  37.        attr1 = Musicvar.getAttribute("songname");
  38.        attr2 = Musicvar.getAttribute("songurl");
  39.         attr3 = Musicvar.getAttribute("id");
  40.             if(attr3 == "newepisode")
  41.             {
  42.                 document.writeln(titles[j].childNodes[0].nodeValue + "<br>");
  43.                 j++;
  44.             }
  45.         document.write("<a href='" + attr2 + "' target='_blank'>" + attr1 + "</a><br>");
  46.         }
  47.     }
  48.  
Thanks for your input Doron, you got me thinking in the right direction. :-)
May 17 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
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 =...
2
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...
3
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>...
1
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...
1
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...
5
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
2
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...
2
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...
2
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'...
0
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...
1
isladogs
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...
0
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...
0
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...
1
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)...
0
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...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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....
0
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

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.