469,327 Members | 1,242 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,327 developers. It's quick & easy.

Accessing CDATA section in XML DOM from Javascript

2
I'm trying to access CDATA inside XML file, works fine in FF but not IE (pretty much all versions). If anyone has any suggestions, please help.
Thanks in advance.

XML:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="ISO-8859-1"?>
  2.  
  3. <CATALOG>
  4.     <CD>
  5.         <ARTIST>Bob Dylan</ARTIST>
  6.         <![CDATA[ <p> Empire Burlesque </p> ]]>
  7.     </CD>
  8.     <CD>
  9.         <ARTIST>Bonnie Tyler</ARTIST>
  10.         <![CDATA[ <p> Hide your heart </p> ]]>
  11.     </CD>
  12.     <CD>
  13.         <ARTIST>Dolly Parton</ARTIST>
  14.         <![CDATA[ <p> Greatest Hits </p> ]]>
  15.     </CD>
  16.     <CD>
  17.         <ARTIST>Gary Moore</ARTIST>
  18.         <![CDATA[ <p> Still got the blues </p> ]]>
  19.     </CD>
  20.     <CD>
  21.         <ARTIST>Eros Ramazzotti</ARTIST>
  22.         <![CDATA[ <p> Eros </p> ]]>
  23.     </CD>
  24.     <CD>
  25.         <ARTIST>Bee Gees</ARTIST>
  26.         <![CDATA[ <p> One night only </p> ]]>
  27.     </CD>
  28.     <CD>
  29.         <ARTIST>Dr.Hook</ARTIST>
  30.         <![CDATA[ <p> Sylvias Mother </p> ]]>
  31.     </CD>
  32.     <CD>
  33.         <ARTIST>Rod Stewart</ARTIST>
  34.         <![CDATA[ <p> Maggie May </p> ]]>
  35.     </CD>
  36.     <CD>
  37.         <ARTIST>Andrea Bocelli</ARTIST>
  38.         <![CDATA[ <p> Romanza </p> ]]>
  39.     </CD>
  40.     <CD>
  41.         <ARTIST>Percy Sledge</ARTIST>
  42.         <![CDATA[ <p> When a man loves a woman </p> ]]>
  43.     </CD>
  44.     <CD>
  45.         <ARTIST>Savage Rose</ARTIST>
  46.         <![CDATA[ <p> Black angel </p> ]]>
  47.     </CD>
  48.     <CD>
  49.         <ARTIST>Many</ARTIST>
  50.         <![CDATA[ <p> 1999 Grammy Nominees </p> ]]>
  51.     </CD>
  52.  
  53. </CATALOG>


HTML:
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3.  
  4. <script type="text/javascript">
  5. var xmlDoc;
  6. if (window.XMLHttpRequest)
  7.   {
  8.   xhttp=new XMLHttpRequest();
  9.   }
  10. else // Internet Explorer 5/6
  11.   {
  12.   xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13.   }
  14. xhttp.open("GET","acctest2.xml",false);
  15. xhttp.send("");
  16. xmlDoc=xhttp.responseXML;
  17.  
  18.  
  19.  
  20. var x=xmlDoc.getElementsByTagName("CD");
  21.  
  22.  
  23. function show(i)
  24. {
  25. artist=(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
  26. title=(x[i].childNodes[3].textContent);
  27. txt="Artist: "+artist+"<br />Title: "+title  ;
  28. document.getElementById("show").innerHTML=txt;
  29. current=i;
  30. }
  31.  
  32. current=0;
  33.  
  34. function next()
  35. {
  36. if (current<x.length-1)
  37.   {
  38.   current++;
  39.   show(current);
  40.   }
  41. }
  42.  
  43. function previous()
  44. {
  45. if (current>0)
  46.   {
  47.   current--;
  48.   show(current);
  49.   }
  50. }
  51.  
  52. </script>
  53. </head>
  54.  
  55. <body onLoad="show(0)">
  56. <div id='show'></div>
  57. <br />
  58. <input type="button" onClick="previous()" value="previous" />
  59. <input type="button" onClick="next()" value="next" />
  60. <br />
  61.  
  62. <br />
  63. <a onclick='show("0"); return false;' href="">item 0</a><br />
  64. <a onclick='show("1"); return false;' href="">item 1</a><br />
  65. <a onclick='show("2"); return false;' href="">item 2</a><br />
  66. <a onclick='show("3"); return false;' href="">item 3</a><br />
  67. <a onclick='show("4"); return false;' href="">item 4</a><br />
  68. <a onclick='show("5"); return false;' href="">item 5</a><br />
  69. <a onclick='show("6"); return false;' href="">item 6</a><br />
  70. <a onclick='show("7"); return false;' href="">item 7</a><br />
  71. <a onclick='show("8"); return false;' href="">item 8</a><br />
  72. <a onclick='show("9"); return false;' href="">item 9</a><br />
  73. <a onclick='show("10"); return false;' href="">item 10</a><br />
  74. <a onclick='show("11"); return false;' href="">item 11</a><br />
  75. <a onclick='show("12"); return false;' href="">item 12</a><br />
  76.  
  77. </body>
  78. </html>
May 4 '10 #1
3 9016
jkmyoung
2,057 Expert 2GB
title=(x[i].childNodes[3].textContent);
Noticed you've hardcoded the 3. Are you trying to get the entire text node, <p> tags and all?

I'm not great with browser versions, but this might work.
title=(x[i].getElementsByTagName("p")[0].nodeValue);
May 4 '10 #2
vm35
2
@jkmyoung
Good call, Thank you! I actually did figured it out about 30 mins ago. Apparently in FF it's title=(x[i].childNodes[3].data and in IE it's title=(x[i].childNodes[1].data. Technically, all I need to do is create and if/else statement for each browser. Your solution would make it much more simple though, I will try it. Thanks again.
vlad
May 4 '10 #3
Dormilich
8,651 Expert Mod 8TB
IE doesn’t count whitespace textnodes (i.e. linebreaks between elements). you could also loop through all childNodes, until you find a CDATA section:
Expand|Select|Wrap|Line Numbers
  1. for (i = x[j].childNodes.length; i--;) {
  2.     if (x[j].childNodes[i] instanceof CDATA) {
  3.         // this is your CDATA node
  4.     }
  5. }
@jkmyoung: I doubt getElementsByTagName() works inside CDATA nodes, since that is unparsed text.

from the XML point-of-view it would make more sense to use namespaces to save the HTML, though I don’t know how good IE is in handling that.
May 5 '10 #4

Post your reply

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

Similar topics

6 posts views Thread by Cade Perkins | last post: by
2 posts views Thread by Steveino | last post: by
4 posts views Thread by werD | last post: by
1 post views Thread by Dariusz Tomoń | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by listenups61195 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.