Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate or insert an xml into a CDATA
    text
    copied!<p>I have been testing something but I can not make it work, here is the problem:</p> <p>I have an xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;history&gt; &lt;name&gt; &lt;age/&gt; &lt;/name&gt; &lt;/history&gt; </code></pre> <p>I would like to insert a CDATA in that XML and the content of the CDATA is part of the xml as well, something like this:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;history&gt; &lt;![CDATA[ &lt;name&gt; &lt;age/&gt; &lt;/name&gt; ]]&gt; &lt;/history&gt; </code></pre> <p>I am using DOMDocument to create the xml:</p> <pre><code> header('Content-Type: text/xml'); $xml = new DOMDocument("1.0","UTF-8"); $base = $xml-&gt;appendChild($xml-&gt;createElement('history')); $name = $base-&gt;appendChild($xml-&gt;createElement('name')); $age = $name-&gt;appendChild($xml-&gt;createElement('age')); $test = $xml-&gt;saveXML(); $xml-&gt;getElementsByTagName('history')-&gt;item(0)-&gt;appendChild($xml-&gt;createCDATASection($test)); $xml-&gt;formatOutput = true; echo $xml-&gt;saveXML(); </code></pre> <p>it generates this xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;history&gt; &lt;name&gt; &lt;age/&gt; &lt;/name&gt; &lt;![CDATA[&lt;?xml version="1.0" encoding="UTF-8"?&gt;&lt;history&gt;&lt;name&gt;&lt;age/&gt;&lt;/name&gt;&lt;/history&gt;]]&gt; &lt;/history&gt; </code></pre> <p>The problems I am facing at the moments are:</p> <ol> <li>The way I look at how CDATA works, it looks like I need to complete all the 'inner xml' before I can cut that 'inner xml' and then create the cdata section and paste it into the cdata section</li> <li>I am using a temporary solution which is str_replace, but I would like to utilize the domdocument createCData function as much as possible.</li> </ol>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload