473,466 Members | 1,354 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

simplexml and CDATA

Hello,

Can someone please enlighten me on how to preserve the <![CDATA[]]>
element when parsing an xml file or string with simplexml. I'm using
libxml 2.6.16 and php 5.1.4.

I tried a few variations, I found at
http://us3.php.net/manual/en/functio...oad-string.php

$xml = simplexml_load_string($newsMLString, 'SimpleXMLElement',
LIBXML_NOCDATA);
echo $xml->asXML();

$xml = simplexml_load_string($newsMLString);
echo $xml->asXML();

$xml = simplexml_load_file('include/newsMLSpecs.xml');
echo $xml->asXML();

$xml = simplexml_load_file('include/newsMLSpecs.xml',
'SimpleXMLElement', LIBXML_NOCDATA);
echo $xml->asXML();

In all cases the CDATA element is stripped out. I don't understand why
an xml parser would do that by default? It could be my setup but not
sure.

I'm using the NewsML spec found here:
http://public.yahoo.com/~ron/news/specs.xml

Any ideas?

Thanks!

Milan

Aug 7 '06 #1
2 14745
ma*****@gmail.com wrote:
>Can someone please enlighten me on how to preserve the <![CDATA[]]>
element when parsing an xml file or string with simplexml. I'm using
libxml 2.6.16 and php 5.1.4.
As far as I've been able to find out this is not possible. However, I
don't think there is a *functional* difference between preserving the
CDATA and what simplexml does. In CDATA sections, you can include e.g.
<i>some</iHTML elements as-is. They, when parsing, will not result in
separate nodes in the resulting DOM. The XML that $xml->asXML()
generates, escapes the reserved characters (<, >, &) instead. As far as
I can tell this results in functionally the same XML.

Of course, it would be nice if simplexml would remember that a given
element originally held CDATA contents and output it as such. Perhaps
you can file an enhancement request for this.

Gertjan.
--
Gertjan Klein <gk****@xs4all.nl>
Aug 8 '06 #2

Gertjan Klein wrote:
ma*****@gmail.com wrote:
Can someone please enlighten me on how to preserve the <![CDATA[]]>
element when parsing an xml file or string with simplexml. I'm using
libxml 2.6.16 and php 5.1.4.

As far as I've been able to find out this is not possible. However, I
don't think there is a *functional* difference between preserving the
CDATA and what simplexml does. In CDATA sections, you can include e.g.
<i>some</iHTML elements as-is. They, when parsing, will not result in
separate nodes in the resulting DOM. The XML that $xml->asXML()
generates, escapes the reserved characters (<, >, &) instead. As far as
I can tell this results in functionally the same XML.

Of course, it would be nice if simplexml would remember that a given
element originally held CDATA contents and output it as such. Perhaps
you can file an enhancement request for this.
I think the answer is use DOM if your needs are not simple. ;)
Which should be available if SimpleXML is.
http://us3.php.net/manual/en/ref.dom.php

Thanks Gertjan.

Aug 8 '06 #3

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

Similar topics

0
by: archi | last post by:
dear all, what would be the most simple way to multisort the (HTML) output from a simplexml file. imagine i have loaded a simplexml file with names & addresses & much more, and i would like...
1
by: hwcowan | last post by:
Hello, I have started using SimpleXML and can do most things, but there are a couple of things that I can't seem to figure out. Currently, I can: 1. Open & load an XML file 2. Manually...
3
by: theboss3 | last post by:
Take, for example a typical RSS 2.0 feed (like http://lorelle.wordpress.com/feed/) and tell me how I access the tags like 'content:encoded' and 'wfw:commentRSS' using SimpleXML. I've searched far...
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);...
1
by: Andy | last post by:
Hi, I'm running in to a little issue with SimpleXML, and wondered if anyone knew if it was normal implementation for SimpleXML, or I'm not using a correct flag, or even it's a an issue that...
7
by: Max | last post by:
Hello everyone! Can anyone help me to convert the CDATA expression "CDATA ::= (Char* - (Char* ']]>' Char*)" to Javascript Regular Expression? Thanks, Max
0
by: ty | last post by:
I have a script that takes a xml template then adds data into it using SimpleXMLElement. I then save it using asXML into a mysql database. On my development machine at home it works fine. ...
7
by: dimo414 | last post by:
So I'm trying to use SimpleXML to get some attribute information about some nodes in my XML document, but it seems like SimpleXML ignores attributes for elements with no children, For instance:...
6
by: dkyadav80 | last post by:
Hi sir, I'm new about xml, javascript. I have two selection field(html) first is city and second is state. the city and state values should be store in xml file. when user select city then all...
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
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...
0
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,...
0
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...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.