Saving CDATA with saveXML in PHP

I wrote a short PHP script to grab an existing XML file, find the node and change it to a new value. The new value should be wrapped in a CDATA block. This works great, however the <> characters are replaced with the corresponding HTML objects. This ruins my XML validation in Java.

<?php
$fileName = "whatever";

$doc = new DOMDocument();
$doc->load('test.xml');
$doc->getElementsByTagName("SomeNode")->item(0)->nodeValue = "<![CDATA[".$fileName."]]>";
header('Content-type: text/xml');
$doc->save("test.xml");
echo $doc->saveXML();

?>

This is the source of the new test.xml:

<?xml version="1.0"?>
<Root>
<FirstNode>
   <SomeNode>&lt;![CDATA[whatever]]&gt;</SomeNode>
</FirstNode>
</Root>

I really need <> characters, not their HTML entities. How can i do this?

UPDATE: I used createCDATASection () as suggested, but it does not work when I try to save it as a node value (I just get a blank page). It works if I add it to the DOM, but it does not do me any good.

$cdata = $doc->createCDATASection( 'whatever' ));
$doc->getElementsByTagName("SomeNode")->item(0)->nodeValue = $cdata;
+4
source share
2 answers

, test.xml

<?xml version="1.0"?>
<Root>
    <FirstNode>
        <SomeNode>a</SomeNode>
    </FirstNode>
</Root>

:
SomeNode

<SomeNode>
    a
    <![CDATA[whatever]]>
</SomeNode>

:

$cdata = $doc->createCDATASection( 'whatever' );
$doc->getElementsByTagName("SomeNode")
    ->item(0)
    ->appendChild($cdata);

SomeNode

<SomeNode>
    <![CDATA[whatever]]>
</SomeNode>

:

$cdata = $doc->createCDATASection( 'whatever' );
$oldNode = $doc->getElementsByTagName("SomeNode")
               ->item(0);
$oldNode->parentNode
        ->replaceChild($cdata,$oldNode);
+2

DOMNode::$nodeValue , . . XML 3 :

1) node 2) node 3) node ()

node

node, .

node:

$element = $document->createElement('nodename');

node:

$text = $document->createTextNode('<content>');

CDATA:

$cdata = $document->createCDATASection('<content>');

node

, node, node.

$document->appendChild($element);
$element->appendChild($text);
$element->appendChild($cdata);

appendChild() - node. create:

$element = $document->appendChild($document->createElement('nodename'));

node

. .

$element->setAttribute('attr', 'value');

// create the document 
$document = new DOMDocument();

// create the document element node and append it
$element = $document->appendChild($document->createElement('nodename'));
// set an attribute on the element node
$element->setAttribute('attr', 'value');
// add a text node to the element node (escape <>)
$element->appendChild($document->createTextNode('<content>'));
// add a CDATA section to the element node (do not escape <>)
$element->appendChild($document->createCDATASection('<content>'));

echo $document->saveXml();

:

<?xml version="1.0"?>
<nodename attr="value">&lt;content&gt;<![CDATA[<content>]]></nodename>

XML

XML, node. XPath.

node. , DOMDocument::getElementByTagName(), node, .

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

foreach ($xpath->evaluate('/Root/FirstNode/SomeNode[1]') as $someNode) {
  $someNode->nodeValue = '';
  $someNode->appendChild($document->createCDATASection('<content>'));
}
echo $document->saveXml(); 

:

<?xml version="1.0"?>
<Root>
  <FirstNode>
    <SomeNode><![CDATA[<content>]]></SomeNode>
  </FirstNode>
</Root>

$someNode->nodeValue = ''. . , . CDATA .

. , CDATA, . CDATA /.

+3

All Articles