Guest User

Untitled

a guest
Sep 23rd, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.82 KB | None | 0 0
  1. <?php
  2.  
  3. $data = <<<XML
  4. <sample>
  5. <content>Inhalt Eins</content>
  6. <content>Inhalt <b>Zwei</b></content>
  7. </sample>
  8. XML;
  9.  
  10. $dom = new DOMDocument();
  11. $dom->loadXml($data);
  12. $xpath = new DOMXpath($dom);
  13.  
  14. // let's look for all content elements
  15. foreach ($xpath->evaluate('//content') as $contentNode) {
  16. // save the fragments into an string
  17. $fragment = '';
  18. foreach ($contentNode->childNodes as $fragmentNode) {
  19. $fragment = $dom->saveXml($fragmentNode);
  20. }
  21. // and create an cdata from that string
  22. $cdataNode = $dom->createCDATASection($fragment);
  23. // remove all children of the content node
  24. while ($contentNode->firstChild) {
  25. $contentNode->removeChild($contentNode->firstChild);
  26. }
  27. // and add the cdata as the only new child node
  28. $contentNode->appendChild($cdataNode);
  29. }
  30.  
  31. header('Content-type: text/xml');
  32. echo $dom->saveXml();
Add Comment
Please, Sign In to add comment