simonschaufi / pretty-xml
Library for pretty-printing XML
Fund package maintenance!
simonschaufi
www.paypal.me/simonschaufi/10
Requires
- php: ^7.4 || ^8.0
- symfony/polyfill-php80: ^1.31
Requires (Dev)
- ergebnis/composer-normalize: ^2.47
- phpunit/phpunit: ^10.5
README
A tiny library for pretty printing XML, inspired purely from DomDocument's lack of ability to configure indent distance.
Usage
Installation
The recommended way to install the extension is using Composer.
Run the following command:
composer require simonschaufi/pretty-xml
How to use
Prettify
To use, give it a badly indented (but well-formed and valid) XML string:
use PrettyXml\Formatter; $formatter = new Formatter(); echo "<pre>" . htmlspecialchars($formatter->format('<?xml version="1.0" encoding="UTF-8"?><foo><bar>Baz</bar></foo>')) . "</pre>";
You can also change the size of the indent:
$formatter->setIndentSize(2);
And you can change the indent character:
$formatter->setIndentCharacter("\t");
Minify
use PrettyXml\Formatter; $formatter = new Formatter(); echo htmlspecialchars($formatter->minify(<<<XML <?xml version="1.0" encoding="UTF-8"?> <foo> <bar>Baz</bar> </foo> XML)); // keep comments echo htmlspecialchars($formatter->minify(<<<XML <?xml version="1.0" encoding="UTF-8"?> <foo> <!-- comment --> <bar>Baz</bar> </foo> XML, true));
Thanks
Thanks to vkBeautify for their algorithm.