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 pear.php.net 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 phpclasses.org 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>”
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.