Page 1 of 1
Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 11:05 am
by LDusan
I would like to add CDATA section declaration inside every tag of an XML document. Something like this:
to become
Is there any predefined function for this? Thanks.
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 11:49 am
by AbraCadaver
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 11:51 am
by LDusan
I know about SimpleXML, but I have not found a function that does exactly what I need.
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 11:53 am
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]}]]>";
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 12:08 pm
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.
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 1:03 pm
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.
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 3:10 pm
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?
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 3:50 pm
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.
Re: Auto add CDATA to every XML tag
Posted: Tue Apr 06, 2010 4:23 pm
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.
Re: Auto add CDATA to every XML tag
Posted: Mon Apr 12, 2010 5:13 pm
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?
Re: Auto add CDATA to every XML tag
Posted: Mon Apr 12, 2010 5:37 pm
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.