Created
July 11, 2013 02:01
-
-
Save ccagle8/5971913 to your computer and use it in GitHub Desktop.
Essential XML utilities in PHP.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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