<?
/** Prettifies an XML string into a human-readable and indented work of art
* @param string $xml The XML as a string
* @param boolean $html_output True if the output should be escaped (for use in HTML)
*/
function xmlpp($xml, $html_output=false) {
$xml_obj = new SimpleXMLElement($xml);
$level = 4;
$indent = 0; // current indentation level
$pretty = array();
// get an array containing each XML element
$xml = explode("\n", preg_replace('/>\s*</', ">\n<", $xml_obj->asXML()));
// shift off opening XML tag if present
if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) {
$pretty[] = array_shift($xml);
}
foreach ($xml as $el) {
if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) {
// opening tag, increase indent
$pretty[] = str_repeat(' ', $indent) . $el;
$indent += $level;
} else {
if (preg_match('/^<\/.+>$/', $el)) {
$indent -= $level; // closing tag, decrease indent
}
if ($indent < 0) {
$indent += $level;
}
$pretty[] = str_repeat(' ', $indent) . $el;
}
}
$xml = implode("\n", $pretty);
return ($html_output) ? htmlentities($xml) : $xml;
}
echo '<pre>' . xmlpp($xml, true) . '</pre>';
?>
XML PHP Pretty Printer
Posted by
Eric (Google)
on
Thursday, November 20, 2008
Labels:
php,
prettyprint,
utility,
xml
Subscribe to:
Post Comments (Atom)
19 comments:
nice code you have.
Hey Eric,
great work. Runs out of the box :-)
Thanks
Excellent code - worked fine for me.
You saved me lots of time - a valuable commodity.
Regards,
Tim
Thanks! Glad this helped.
Eric
Hey Eric, short and sweet code.
Thanks :)
Excellent, thanks a lot!
This function helped me out a lot. Thanks!
can anyone please tell me how do i use this function?
brilliant... nice and easy!
Thx for the code!
Funciona perfectamente!
Would you be willing to put a license on this code? public-domain? GPL? bsd? I'd like to use it, but legally can't until you do!
At the bottom:
All code licensed under the Apache 2.0 License.
I might be missing something really obvious, but i think if you leave out the declaration of the SimpleXMLEelement and use
$xml = explode("\n", preg_replace('/>\s*\n<", $xml));
instead, you get the same reults...
socks5 | socks proxy | proxy free | proxy
socks proxy | socks5 | proxy | proxy free
Girl Xinh | Hot Girl | Cute Girl | Sexy Girl | Hot Girl Asian
Hot Girl | Lexi Belle | Sexy Girl | Cute Girl | Hot Asian Girl | Leah Dizon | Hot Girl Asian | Girl Xinh
socks proxy | socks5 | proxy free |proxy list
healthtips
healthtips
healthtips
healthtips
healthtips
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
socks proxy
check socks proxy
socks proxy
socks proxy
funnystory
funnystory
funnystory
funnystory
funnystory
funny pictures
funny pictures
funny pictures
funny pictures
funny pictures
funny pictures
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
Girl Xinh
cute animal
food news
internet
football news
bank news
marketing
science news
travel vietnamt
real estate vietnam
business news
music news
health news
software
seo news
Girl Xinh | Hot Girl | Cute Girl | Sexy Girl |Hot Girl Asian | Hot Girl Xinh
Funny Story | Funny Stories | Funny | lol
Funny Pictures | funny | lol
Girl Xinh | Hot Girl | Cute GirlSexy Girl |Hot Girl Asian | Hot Girl Xinh | lexi belle | ngoc trinh
In PHP versions prior to 5.3 make sure to set encoding and other options for htmlentities.
thanks for this great tip... :)
delicious
Post a Comment