Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $data = <<<XML
- <sample>
- <content>Inhalt Eins</content>
- <content>Inhalt <b>Zwei</b></content>
- </sample>
- XML;
- $dom = new DOMDocument();
- $dom->loadXml($data);
- $xpath = new DOMXpath($dom);
- // let's look for all content elements
- foreach ($xpath->evaluate('//content') as $contentNode) {
- // save the fragments into an string
- $fragment = '';
- foreach ($contentNode->childNodes as $fragmentNode) {
- $fragment = $dom->saveXml($fragmentNode);
- }
- // and create an cdata from that string
- $cdataNode = $dom->createCDATASection($fragment);
- // remove all children of the content node
- while ($contentNode->firstChild) {
- $contentNode->removeChild($contentNode->firstChild);
- }
- // and add the cdata as the only new child node
- $contentNode->appendChild($cdataNode);
- }
- header('Content-type: text/xml');
- echo $dom->saveXml();
Add Comment
Please, Sign In to add comment