Auto add CDATA to every XML tag

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Auto add CDATA to every XML tag

Post by LDusan »

I would like to add CDATA section declaration inside every tag of an XML document. Something like this:

Code: Select all

<tag>content</tag>
to become

Code: Select all

<tag><![CDATA[content]]></tag>
Is there any predefined function for this? Thanks.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Auto add CDATA to every XML tag

Post by AbraCadaver »

SimpleXML will work for this: http://us3.php.net/manual/en/book.simplexml.php

Also DOMDocument which is more feature rich: http://us3.php.net/manual/en/book.dom.php
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Auto add CDATA to every XML tag

Post by LDusan »

I know about SimpleXML, but I have not found a function that does exactly what I need.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Auto add CDATA to every XML tag

Post by AbraCadaver »

LDusan wrote:I know about SimpleXML, but I have not found a function that does exactly what I need.

Code: Select all

$xml->tag[0] = "<![CDATA[{$xml->tag[0]}]]>";
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Auto add CDATA to every XML tag

Post by LDusan »

Thanks, but I need solution for the case when the XML structure is unknown, when I don't know which tags the document has.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Auto add CDATA to every XML tag

Post by AbraCadaver »

LDusan wrote:Thanks, but I need solution for the case when the XML structure is unknown, when I don't know which tags the document has.
You'll have to be more specific, but what about:

Code: Select all

foreach ($xml->children() as $tag => $value) {
	$xml->$tag = "<![CDATA[$value]]>";
}
SimpleXML has some problems with CDATA though, so you may want to use DOMDocument.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Auto add CDATA to every XML tag

Post by requinix »

Unless the thing parsing the XML has bugs, you don't need a CDATA block.

Could you explain why you think you do?
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Auto add CDATA to every XML tag

Post by LDusan »

Thanks, I am using SimpleXML to make bilingual site. All content is in one XML and in tags. Some tags have html tags inside them and some don't. Those that do have html tags do not process well without CDATA block, that's the whole problem.

Code: Select all

$lang = simplexml_load_file('content.xml');
echo $lang->$choice->section;
Thanks for help.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Auto add CDATA to every XML tag

Post by requinix »

Encode the HTML, like

Code: Select all

<node>This is some <b>HTML</b></node>
It's the same as

Code: Select all

<node><![CDATA[This is some <b>HTML</b>]]></node>
Tends to be that you'll have more problems with CDATA blocks than if you encode the tags. If I remember correctly, to get SimpleXML to handle CDATAs you actually have to set some parse flags - not handled out-of-the-box.
LDusan
Forum Commoner
Posts: 45
Joined: Sun Mar 08, 2009 5:03 am
Location: Belgrade, Serbia

Re: Auto add CDATA to every XML tag

Post by LDusan »

Thanks a lot, basically, what is the best way to make a system that processes just one level of tags?

For example:

Code: Select all

<node1>text</node1>
<node2><span>some text</span></node2>
<node3>text</node3>
I want the system to regard only node1, node2, node3 as tags, not <span> tag. With SimpleXML I would have problems with node2.

Do I have to make the function that rewrites the whole XML?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Auto add CDATA to every XML tag

Post by requinix »

If you use tags in <>s, without a CDATA block, then they're considered part of the XML. One way or another, if you want them as pure data something will have to be rewritten.

'Course, I don't know of anything that forces you to do this. SimpleXML can parse the <span> correctly - it's just a matter of you having the right code to get what you want.
Post Reply