This question already has an answer here:
The title pretty much says it all.
If I have something like (from PHP site examples):
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies></movies>
XML;
$sxe = new SimpleXMLElement($xmlstr);
$sxe->addAttribute('type', 'documentary');
$movie = $sxe->addChild('movie');
$movie->addChild('title', 'PHP2: More Parser Stories');
$movie->addChild('plot', 'This is all about the people who make it work.');
$characters = $movie->addChild('characters');
$character = $characters->addChild('character');
$character->addChild('name', 'Mr. Parser');
$character->addChild('actor', 'John Doe');
$rating = $movie->addChild('rating', '5');
$rating->addAttribute('type', 'stars');
echo("<pre>".htmlspecialchars($sxe->asXML())."</pre>");
die();
I end up outputing a long string like so:
<?xml version="1.0" standalone="yes"?>
<movies type="documentary"><movie><title>PHP2: More Parser Stories</title><plot>This is all about the people who make it work.</plot><characters><character><name>Mr. Parser</name><actor>John Doe</actor></character></characters><rating type="stars">5</rating></movie></movies>
This is fine for a program consumer, but for debugging/human tasks, does anyone know how to get this into a nice indented format?
Found a similar solution...to format raw xlm data..from my
php SOAP
requests__getLastRequest & __getLastResponse
, for quick debugging the xml's i have combined it withgoogle-code-prettify
.Its a good solution if you want to format sensitive xml data and don't want to do it online.
Some sample code below, may be helpful to others:
Below is a sample of the Formatted XML Output I got:
Note: The formatted XML is available in
$dom->saveXML()
and can be directly saved to a xml file using php file write.There's a variety of solutions in the comments on the PHP manual page for SimpleXMLElement. Not very efficient, but certainly terse, is a solution by Anonymous
The PHP manual page comments are often good sources for common needs, as long as you filter out the patently wrong stuff first.
The above didn't work for me, I found this worked: