The Developer Day | Staying Curious



XML beautifier tool

This post describes a tool that I wrote long time ago. By now I have published a new refactored version of the XML beautifier which solves a few problems of the original tool.

I’m currently working with a system that is communicating with various third parties using web services. There are times when I need to view some of the XMLs or output them for some particular reason. Because of the last reason I wanted the beautifier to be written in PHP so that I could implement it easily. After spending some time googling with keywords like “php xml indent”, “php xml output”, “php xml human readable”, “php xml beautifier” I found that there are some code snippets or incomplete /not fully working classes / functions.

I’ve also tried and there indeed is a package named “XML Beautifier” that is no longer maintained. And I was frightened away by lot’s of dependencies: PEAR, XML Parser, XML Utilities, XML Beautifier .. Brrrr ..

Finally I visited and found a class named “Beauty XML”. It’s simple, works but has a few bugs. So I modified the class to do the following things:

  • To ignore adding a new level after if finds a tag like <tag />
  • To remove all XML headers
  • To remove white spaces from situations like “</tag2> </tag1>”

You can download the beauty XML or try it out yourself.

I think there still may be some XMLs that don’t get indented nicely and if i don’t find out that myself maybe You will. So please tell me if you do. I’ve also added a simple interface file to view XMLs in your web browser.

Maybe you already have or know a tool that does this job perfectly and I would be happy if you shared this with me.

RSS Feed

18 Comments for XML beautifier tool

Florian | July 24, 2007 at 10:54 AM

A nice lank script. Brilliant.

Chris | November 20, 2007 at 1:26 AM

I recommend this change.
//$newstr = preg_replace(“/>([\s]+)([\s]+)

currently the above would become:

instead of:

Manni | April 10, 2008 at 8:41 PM

Nice script, thank you!

But it has some problems with which causes indentation but works like a self closing tag….


Dustin Boston | June 20, 2008 at 7:02 AM

Thanks so much for this script. I’ve been looking for something like it for a while now! Great job!

jerry | January 26, 2009 at 9:57 AM

nice tool you got there bro. thanks for this!

WASD42 | February 26, 2009 at 1:41 PM

Thank you very much, very useful ;)

The Developer Day » Blog Archive » PHP XML formatter tool rewrite | March 31, 2009 at 4:40 PM

[...] while ago I blogged about an XML Beautifier Tool which is able to tokenize an XML string and output it in human readable format. Strangely enough I [...]

Christian V. Sciberras | May 16, 2009 at 1:23 PM

Nice script, however…

I’ve got no idea why people don’t code with “show errors” turned on. Running this on my trusty WAMP server on PHP 4 and 5, I got several errors (undefined variables and undefined offsets in arrays).

Well, needless to argue, I’ve fixed all bugs (that I could see), and now it works smoother.
You can get my version here:

The changes I’ve made are to fix those problems, nothing else. Mentioning me anywhere is welcomed, but not required. Of course, you can copy the relevant parts (all of the code) into yours.

Kind regards,
Christian Sciberras.

a | May 17, 2009 at 1:20 PM

breaks well-formed xml screws up the line endings, but thnx 4 trying

Author comment by admin | May 18, 2009 at 8:54 AM

I’ve wrote a new version for PHP5 that uses SAX instead. Check it out.

Free and open source XML beautifers to format XML at Compdigitec Labs | December 26, 2009 at 2:06 PM

[...] PHP XML beautifier - Small PHP based XML beautifier. The only one that actually worked properly and formatted readable XML. Formatted <tag1><tag2><tag3>xyz</tag3></tag2></tag1> into [...]

Chris | April 14, 2010 at 12:47 PM

I just started writing a simple PHP XML beautifier then I decided to check google first. Works out of the box and is very streamlined. Thanks!

Balaram Singh | June 16, 2010 at 9:08 AM

works great!

Le Do Hoang Long | July 8, 2011 at 8:48 AM

Nice tool! :)

ADitya | February 21, 2012 at 9:45 AM

Nice Post… looking for it and found exactly. Thanks for the Post.. Well your words “I think there still may be some XMLs that don’t get indented nicely and if i don’t find out that myself maybe You will. So please tell me if you do. I’ve also added a simple interface file to view XMLs in your web browser.”… I have an XML file. This script intended nicely over 80% of my file. But the rest 20% is like expanding tree structure. Any clue about it?


Juergen | August 24, 2012 at 6:49 AM

Simple, Easy, exactly what i needed. Thank you for sharing this!

Rajesh | September 29, 2013 at 1:22 PM

I think this is the best XML beautifier on Internet i’ve came across. It also works for SOAP.

Would you like to share source code?

Author comment by Žilvinas Šaltys | January 15, 2014 at 11:49 AM

It’s available for download Rajesh :) Use it any way you want.

Leave a comment!



Find it!

Theme Design by