simonschaufi/pretty-xml

Library for pretty-printing XML

Maintainers

👁 simonschaufi

Package info

github.com/simonschaufi/pretty-xml

pkg:composer/simonschaufi/pretty-xml

Statistics

Installs: 276 786

Dependents: 1

Suggesters: 0

Stars: 22

Open Issues: 0

3.0.0 2025-05-02 09:09 UTC

Requires

Requires (Dev)

Suggests

None

Provides

None

Conflicts

None

Replaces

None

MIT 2af716465f67cac264785cf1ca1484ce054c0f23

xmlpretty

This package is auto-updated.

Last update: 2026-06-12 12:21:41 UTC


README

A tiny library for pretty printing XML, inspired purely from DomDocument's lack of ability to configure indent distance.

👁 Latest Stable Version
👁 Total Downloads

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.