note 92051 added to function.simplexml-element-asXML
| From: | HenryMerriam at osu1 dot php dot net | Date: | Tue, 07 Jul 2009 04:09:25 +0000 |
| Subject: | note 92051 added to function.simplexml-element-asXML | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-157998@lists.php.net to get a copy of this message | ||
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?
----
Server IP: 209.41.74.194
Probable Submitter: 69.19.14.17 (proxied: 72.168.228.129)
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-element-asXML.php
Edit -- https://master.php.net/note/edit/92051
Del: integrated -- https://master.php.net/note/delete/92051/integrated
Del: useless -- https://master.php.net/note/delete/92051/useless
Del: bad code -- https://master.php.net/note/delete/92051/bad+code
Del: spam -- https://master.php.net/note/delete/92051/spam
Del: non-english -- https://master.php.net/note/delete/92051/non-english
Del: in docs -- https://master.php.net/note/delete/92051/in+docs
Del: other reasons-- https://master.php.net/note/delete/92051
Reject -- https://master.php.net/note/reject/92051
Search -- https://master.php.net/manage/user-notes.php