note 77816 added to function.simplexml-element-asXML
| From: | gary dot malcolm at gmail dot com | Date: | Thu, 13 Sep 2007 22:15:44 +0000 |
| Subject: | note 77816 added to function.simplexml-element-asXML | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-130483@lists.php.net to get a copy of this message | ||
I extended SimpleXml and added this code to give me pleasant source code XML. I'm sure
it's ugly so if you can do it better go ahead...
<?php
public function asPrettyXML()
{
$string = $this->asXML();
/**
* put each element on it's own line
*/
$string =preg_replace("/>\s*</",">\n<",$string);
/**
* each element to own array
*/
$xmlArray = explode("\n",$string);
/**
* holds indentation
*/
$currIndent = 0;
/**
* set xml element first by shifting of initial element
*/
$string = array_shift($xmlArray) . "\n";
foreach($xmlArray as $element) {
/** find open only tags... add name to stack, and print to string
* increment currIndent
*/
if (preg_match('/^<([\w])+[^>\/]*>$/U',$element)) {
$string .= str_repeat(' ', $currIndent) . $element . "\n";
$currIndent += self::indent;
}
/**
* find standalone closures, decrement currindent, print to string
*/
elseif ( preg_match('/^<\/.+>$/',$element)) {
$currIndent -= self::indent;
$string .= str_repeat(' ', $currIndent) . $element . "\n";
}
/**
* find open/closed tags on the same line print to string
*/
else {
$string .= str_repeat(' ', $currIndent) . $element . "\n";
}
}
return $string;
}
?>
Regards,
Gary Malcolm
----
Server IP: 208.69.120.35
Probable Submitter: 64.112.238.244
----
Manual Page -- http://www.php.net/manual/en/function.simplexml-element-asXML.php
Edit -- https://master.php.net/note/edit/77816
Del: integrated -- https://master.php.net/note/delete/77816/integrated
Del: useless -- https://master.php.net/note/delete/77816/useless
Del: bad code -- https://master.php.net/note/delete/77816/bad+code
Del: spam -- https://master.php.net/note/delete/77816/spam
Del: non-english -- https://master.php.net/note/delete/77816/non-english
Del: in docs -- https://master.php.net/note/delete/77816/in+docs
Del: other reasons-- https://master.php.net/note/delete/77816
Reject -- https://master.php.net/note/reject/77816
Search -- https://master.php.net/manage/user-notes.php