473,809 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 17559
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.inne rHTML = cdataSectionNod e.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 "responseXM L" property.

2. Use DOM objects and methods (document.first Child 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
12793
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
6199
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> <tag><]></tag>
6
8349
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...
4
4775
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Diagnostics> <ErrorCode>0</ErrorCode> <ErrorDescription>OK</ErrorDescription>
11
6472
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);
12
4017
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 //<!]> Is this trick grounded in any real information about HTML vs XHTML? I
3
6385
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 application="readernews" type="RESPONSE"> <services> <service name="getUserAndDLNames" response-type="JSON" status="SUCCESS"> <message>
9
3267
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
13345
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 StreamReader(Registry.GetValue(@"HKEY_LOCAL_MACHINE\software\myCompany\configDir", "path",@"C:\ConfigDir\") + "ConnectedPCs.xml"));
0
9721
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
9602
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
10639
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
10376
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...
1
10383
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7661
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
6881
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();...
0
5550
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...

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.