Very often I find myself writing input filters for large XML files using PHP. Common enough task; and PHP offer a great variety of tools to do this effectively depending on the situation. Unfortunately, almost as common is the lack of documentation for the aforementioned XML files.
Manually trying to document the XML structure is not a fun job. And I have looked around for a simple tool but I didn’t really find a tool that gave me the quick and dirty overview I wanted. A year or so ago I finally wrote a small PHP class to analyze large XML files.
It is simple, but quickly creates a good overview of the XML structure as well as the kind of data and attributes of the elements in the XML file. Even for a very large XML file it does a good job of showing just the relevant structure of the XML.
It is best described using an example.
<?xml version="1.0" encoding="UTF-8" ?> <company xmlns:dvoidcomp="http://dotvoid.se/schema/company" name="MegaCorp" employeeOfTheMonth="E0003"><department name="Advanced Technologies" location="NY" number="123"><employee name="Al Smith" SN="E0004" manager="true"><title>Tech manager</title></employee><employee name="John Jones" SN="E0001"><title>Worker</title></employee><employee name="Jane Doe" SN="E0003"><title>Worker</title></employee></department><department name="Resarch & Development" location="JS" number="789"><employee name="Anders Andersson" SN="E0005"><title>Lead technologist</title></employee><employee name="Joe Schmoe" SN="E0008" manager="true"/><employee name="Sven Svensson" SN="E0006"><title>Worker</title></employee></department></company>
Example output in HTML
The output shows the overall structure of all possible elements and all possible attributes of the different elements. All attributes have example values so that you can see what kind of data you can expect. In this example I have also asked for all possible values of the title element to be included. This is good if you know there are XML elements with a limited set of values.
Usage of the PHP class
The class is simple to use. And really the only thing you can do is give the path to an XML file and call parse() with an optional array of XML element names that you want all values for in the HTML output.
s<?php use Void\File\XmlStructure; require __DIR__ . '/XmlStructure.php'; $xmlStruct = new XmlStructure(); $xmlStruct->parse( __DIR__ . '/XmlStructure.xml', array('title') ); ?> <html> <head> <style type="text/css"> <?php echo $xmlStruct->css();?> </style> </head> <body> <?echo $xmlStruct->html();?> </body> </html>
Eventually I might, and might not, put it on Bitbucket or Github. The PHP class is not well documented, but small enough. Bare in mind I wrote it quickly and I only use it as a simple means of boiling down a large XML file to the bare essentials. Hopefully you will find it useful or maybe give you ideas on how to improve it.