simplexml and CDATA

This is a discussion on simplexml and CDATA within the PHP Language forums, part of the PHP Programming Forums category; Hello, Can someone please enlighten me on how to preserve the <![CDATA[]]> element when parsing an xml file ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-07-2006
mandric@gmail.com
 
Posts: n/a
Default 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

Reply With Quote
  #2 (permalink)  
Old 08-08-2006
Gertjan Klein
 
Posts: n/a
Default Re: simplexml and CDATA

mandric@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</i> HTML 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 <gklein@xs4all.nl>
Reply With Quote
  #3 (permalink)  
Old 08-09-2006
mandric@gmail.com
 
Posts: n/a
Default Re: simplexml and CDATA


Gertjan Klein wrote:
> mandric@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</i> HTML 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.

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 05:38 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0