2

I have an XML file which contains ![CDATA[]] data. Like this:

<link><![CDATA[https://google.de]]></link>

Now I heard that I can not modify ![CDATA[]] data or that they contains some special characters. But I do not remember anymore... That's the reason why I'am asking here.

Can I change the values in ![CDATA[]] and if yes, how?

I just want to append something like "?=dadc" on the link.

Edit: My XML file structure (Want to edit the url):

<?xml version="1.0" encoding="UTF-8"?>
    <rss>
      <channel xmlns:g="http://base.google.com/ns/1.0" version="2.0">
        <title>Google Eur English 1</title>
        <description/>
        <item>
          <title>Anno 2070</title>
          <g:image_link><![CDATA[http://cdn.kinguin.net/media/catalog/category/anno_8.jpg]]></g:image_link>
          <url><![CDATA[http://www.kinguin.net/category/4/anno-2070/?nosalesbooster=1&country_store=1&currency=EUR]]></url>
          <price><![CDATA[3.88 EUR]]></price>
          <platform>Uplay</platform>
        </item>
      </channel>
    </rss>

Greetings

Jan
  • 277
  • 1
  • 6
  • 16

2 Answers2

4

That is true for SimpleXML. CDATA Sections are a special kind of text nodes. They are actually here to make embedded parts more readable for humans. SimpleXML does not really handle XML nodes so you will have to let it convert them to standard text nodes.

If you have a JS or HTML fragment in XML it is easier to read if the special characters like < are not escaped. And this is what CDATA sections are for (and some backwards compatibility for browsers).

So to modify a CDATA section and keep it, you will have to use DOM. DOM actually knows about the different node types. Here is a small example:

$xml = '<link><![CDATA[https://google.de]]></link>';

$document = new DOMDocument();
$document->loadXml($xml);
$xpath = new DOMXpath($document);

foreach ($xpath->evaluate('//link/text()') as $linkValue) {
  $linkValue->data .= '?abc';
}
echo $document->saveXml();

Output:

<?xml version="1.0"?>
<link><![CDATA[https://google.de?abc]]></link>
ThW
  • 19,120
  • 3
  • 22
  • 44
  • I tried to edit my CDATA values just with getting and appending my stuff but now my links looks like that: https://drive.google.com/file/d/0BzLxINxZFzoveWR3Qi1XelFjM0E/view?usp=sharing I know that you have the right version and I will implement that like you. But what is the exactly difference between CDATA and `&`? – Jan Feb 20 '17 at 21:57
  • 1
    For some caller using a DOM or SimpleXML API here is no difference at the reading level. They should read exactly the same data as a string. The difference is in the serialization. CDATA sections need less escaping so the fragment inside them is more readable for humans. It makes no difference for a receiving XML parser. Here is a small example that shows the difference: https://eval.in/740419 – ThW Feb 20 '17 at 22:29
  • I get "Internal Server Error" if I follow your link. So it makes no difference if there is a `&` or a `&`? - And the link will work or will it be destroyed? – Jan Feb 21 '17 at 07:48
  • `&` is serialized to `&` in XML if needed. Inside a CDATA section it is not. The `&` from a text node and the `&` from a CDATA section are both read as a `&` by the XML parser. – ThW Feb 21 '17 at 09:48
  • And also in the browser? - Because it is an URL link and I want that it is working. But if it is working also with `&` I don not need to make me more work :D – Jan Feb 21 '17 at 13:45
  • Yes, the XML/HTML parser in the browser will act as I described. However an `&` in a CDATA section will be read as `&`. – ThW Feb 21 '17 at 14:05
  • I now tried your way but I m not successful... I do not know what I have to type in the `$xpath->evaluate(...)` path... I updated my question. There you can see my XML file structure. Could you maybe say me what I have to type into the `evaluate` function? – Jan Feb 25 '17 at 23:53
  • Could you help me pls? – Jan Feb 26 '17 at 09:58
0

Fortunately yes!, check this:

$link = simplexml_load_string(
    '<link><![CDATA[Hello, world!]]></link>'
);

echo (string)$link;

PHP: How to handle <![CDATA[ with SimpleXMLElement?

Regards, Idir

Community
  • 1
  • 1
Idir Ouhab Meskine
  • 587
  • 1
  • 9
  • 23