473,597 Members | 2,731 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing CDATA section in XML DOM from Javascript

2 New Member
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 9429
jkmyoung
2,057 Recognized Expert Top Contributor
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].getElementsByT agName("p")[0].nodeValue);
May 4 '10 #2
vm35
2 New Member
@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,658 Recognized Expert Moderator Expert
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 getElementsByTa gName() 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

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

Similar topics

2
6485
by: nowhere | last post by:
Hi, I need to preserve some ASCII control characters (CR and LF) within an XML file so I have included the data in a CDATA section. However, when parsing it using expat, I lose the CR characters. My question is: Should I be using a different character set (not UTF-8) or is this a bug in expat? TIA, Mark
6
8337
by: Cade Perkins | last post by:
How can the CDATA ending delimiter "]]>" be represented within a CDATA section itself? Consider an XML document that is intended to contain an embedded, uninterpreted XML example. Generally, the easiest way to represent it would be to put the embedded XML example inside a CDATA section. But if the example has a CDATA section itself, then the ending delimiter will be interpreted as the end of the "real" CDATA section. Here's even a...
10
42951
by: Jon Noring | last post by:
Out of curiosity, may a CDATA section appear within an attribute value with datatype CDATA? And if so, how about other attribute value datatypes which accept the XML markup characters? To me, the XML specification seems a little ambiguous on this, so I defer to the XML authorities. Refer to sections 2.4 and 2.7 (it all hinges on if CDATA attribute values are part of markup or not.) Thanks.
11
6438
by: ericms | last post by:
Can anybody show me how to insert a CDATA section using XPathNavigator ? I have tried the follwing with no luck: XmlDocument docNav = new XmlDocument(); docNav.LoadXml(xmlString); XPathNavigator nav = docNav.CreateNavigator(); XmlDocument doc = new XmlDocument(); doc.LoadXml("<DocumentData></DocumentData>"); XmlElement elem = doc.CreateElement(currentNodeName);
2
5237
by: Steveino | last post by:
Hello, Just wondering if anyone could shed any light on this, it's probably me just being silly... I have a dataset that I've used to create an XmlDataDocument, in order to apply XSL. The XSL reformats the XML to another format (still XML, not HTML). This works fine, but whatever I do, I can't get the finally output section to have CDATA elements. I've added a cdata-section-elements tag into the xsl file, but it doesn't seem to have...
4
4947
by: werD | last post by:
Hello, Ive been reading about cdata-section-elements b/c ive got a comments field that id like to inlcude some html in. For some reason the final output is not including any cdata tags though? The original field that this is pulled from does use cdata tags but they dont seem to come through Is this possible or is there something im missing?
2
17530
by: Pugi! | last post by:
Using AJAX I want to send some information from the server (php-page) as XML to the client. The contents can be very divers so I have to use XML instead of text. On one occasion the contents is html, I can put this in a CDATA section with PHP, but how do I retrieve that information with Javascript and put the HTML in a div ? thanx, Pugi!
1
4237
by: Dariusz Tomoń | last post by:
Hi, I have got xml document with CDATA sections containing special characters like links to images. All Iwant is to display the content withing my div section. Can you provide me a snippet of a code. Thank you in advance. Darek T.
1
4079
!NoItAll
by: !NoItAll | last post by:
The MSXML parser is choking on a single character that often appears in my data within a CDATA section. Try this: Dim bRet As Boolean Dim lRet as long Dim xmlDoc As MSXML2.DOMDocument Set xmlDoc = New MSXML2.DOMDocument
0
7977
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7894
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,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
jinu1996
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...
0
8261
tracyyun
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...
1
5847
isladogs
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...
0
5437
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();...
1
1496
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1245
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.