Skip to content

Instantly share code, notes, and snippets.

@ccagle8
Created July 11, 2013 02:01
Show Gist options
  • Save ccagle8/5971913 to your computer and use it in GitHub Desktop.
Save ccagle8/5971913 to your computer and use it in GitHub Desktop.
Essential XML utilities in PHP.
<?php
/**
* SimpleXMLExtended Class
*
* Extends the default PHP SimpleXMLElement class by
* allowing the addition of cdata
*
* @param string $cdata_text
*/
class SimpleXMLExtended extends SimpleXMLElement{
public function addCData($cdata_text){
$node= dom_import_simplexml($this);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection($cdata_text));
}
}
/**
* Get XML
*
* Returns array of XML file contents
*
* @param string $file
* @return array
*/
function getXML($file) {
$xml = file_get_contents($file);
$data = simplexml_load_string($xml, 'SimpleXMLExtended', LIBXML_NOCDATA);
return $data;
}
/**
* XML Save
*
* @param object $xml
* @param string $file Filename that it will be saved as
* @return bool
*/
function XMLsave($xml, $file) {
$success = $xml->asXML($file) === TRUE;
return $success && chmod($file, 0755);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment