473,387 Members | 1,904 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,387 software developers and data experts.

How to read (get contents) of CDATA section in XML with Javascript ?

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!

Dec 11 '06 #1
2 17477
Pugi! wrote:
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 ?
A CDATA section shows up as a CDATA section node (nodeType is 4) in the
DOM where the contents of the CDATA section can be accessed as e.g.
node.nodeValue
or
node.data
A CDATA section node is always a leaf node so you will find it as a
child nodes of element nodes. You can also use XPath where supported to
find the CDATA section node, XPath does not distinguish between text
nodes and CDATA section nodes, both are selected by the XPath text().
So grab the CDATA section node and put its nodeValue as the innerHTML of
a HTML div e.g.
divElement.innerHTML = cdataSectionNode.nodeValue;

--

Martin Honnen
http://JavaScript.FAQTs.com/
Dec 11 '06 #2

Pugi! wrote:
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!
Hi

Assuming you are using XMLHttpRequest, a common approach is:-

1. Get the XMLDocument from the "responseXML" property.

2. Use DOM objects and methods (document.firstChild etc) to get to the
CDATASection in the node tree, and "nodeValue" to extract its contents.

3. Use "innerHTML" to put the contents in a div.

There are lots of libraries to choose from that offer to do this for
you, as a small amount of Google searching will show. Sarissa is one
such example.

Regards

Julians

Dec 11 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Tjerk Wolterink | last post by:
Hello all, I've a problem: i've an xsl file that has a template that contains the following: <script type="text/javascript"> <!]>> </script>
3
by: Balaras | last post by:
Hi, Can sombody here please help me a bit with a regular expression. I have a xml file where I need to strip the CDATA sections of any contained data. Eg. <xml> <tag><]></tag>...
6
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,...
4
by: Rich Wallace | last post by:
Hi all, I have an XML document fed to me from a third party app: <?xml version="1.0" encoding="WINDOWS-1252" ?> <GatewayPlan xmlns:xsd="http://www.w3.org/2001/XMLSchema"...
11
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);...
12
by: Peter Michaux | last post by:
Hi, I am experimenting with some of the Ruby on Rails JavaScript generators and see something I haven't before. Maybe it is worthwhile? In the page below the script is enclosed in //<!]> ...
3
by: satyajit123 | last post by:
HI All, I have a requirement which has an XML with CDATA content in it is to be passed through an XSL for transformation to HTML tags. The xml is of below format. <xml-service...
9
by: shapper | last post by:
Hello, Why do some pages I have seen have //<![CDATA[ in the beginning of a script tag before the script itself? Do I need this? Thanks, Miguel
2
by: viepia | last post by:
Hi, How would I edit the code below to read XML CData text? Thanks, Viepia XDocument connectedPCsXML = XDocument.Load(new...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...
0
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...

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.