By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,741 Members | 2,033 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,741 IT Pros & Developers. It's quick & easy.

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

P: 63
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
Share this Question
Share on Google+
7 Replies


dorinbogdan
Expert 100+
P: 839
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
Expert 100+
P: 839
Or you can use XPath with selectNodes() method.
See this link, and related chapters there.
May 16 '07 #3

P: 63
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
Expert 100+
P: 839
I understand.
Let me try a test, using XPath, and I'll come back with the result.
May 16 '07 #5

dorinbogdan
Expert 100+
P: 839
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

P: 63
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

P: 63
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

Post your reply

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