Skip to content

Instantly share code, notes, and snippets.

@dnaber-de
Created August 28, 2012 17:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dnaber-de/3501167 to your computer and use it in GitHub Desktop.
Save dnaber-de/3501167 to your computer and use it in GitHub Desktop.
This Class extends SimpleXMLElement to write CDATA-Nodes and print a formated xml-string
<?php
/**
* This Class extends SimpleXMLElement to write CDATA-Nodes and print a formated xml-string
*
* to add cdata do it like this:
* ($node is an instance of Not_So_Simple_XML)
* $node->title = NULL;
* $node->title->addCData( );
*
* @author David Naber <kontakt@dnaber.de>
* @version 2012.08.28
* @url https://gist.github.com/3501167
* @link http://coffeerings.posterous.com/php-simplexml-and-cdata
*/
class Not_So_Simple_XML extends SimpleXMLElement {
/**
* adds a cdata node
*
* @param string $cdata_text
* @return void
*/
public function add_cdata( $cdata_text ) {
$node = dom_import_simplexml( $this );
$no = $node->ownerDocument;
$node->appendChild( $no->createCDATASection( $cdata_text ) );
}
/**
* returns a formatet xml string
*
* @return string
*/
public function as_formated_xml() {
$xml_string = $this->asXML();
$dom = new DOMDocument( '1.0', 'UTF-8' );
$dom->loadXML( $xml_string );
$dom->preserveWhiteSpace = FALSE;
$dom->formatOutput = TRUE;
return $dom->saveXML();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment