note 92051 deleted from function.simplexml-element-asXML by thiago
| From: | thiago@php.net | Date: | Tue, 07 Jul 2009 12:02:16 +0000 |
| Subject: | note 92051 deleted from function.simplexml-element-asXML by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-158009@lists.php.net to get a copy of this message | ||
Note Submitter: Henry Merriam
----
Using the method posted by administrator at proxy-list dot org I have extended the SimpleXMLElement
class to output 'pretty' XML automagically.
<?php
class SimpleXMLPretty extends SimpleXMLElement
{
public function asXML($filename = null) {
$doc = new DOMDocument('1.0');
$doc->preserveWhiteSpace = false;
$doc->loadXML(parent::asXML());
$doc->formatOutput = true;
$xml = new parent($doc->saveXML());
if (!$filename) {
return $xml->asXML();
} else {
return $xml->asXML($filename);
}
}
}
// Example usage:
$xml = new SimpleXMLPretty($xmlstr);
echo $xml->asXML(); // output pretty xml
$xml->asXML('foo.xml'); // saving as a file works too
?>
Comments are appreciated. Should the function be declared as static? Is there a better way to handle
the file save feature?