News Archive
PhpRiot Newsletter
Your Email Address:

More information

Getting to grips with an existing XML structure

Note: This article was originally published at Planet PHP on 24 April 2012.
Planet PHP

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 Atool 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.

Example XML

A Tech manager Worker Worker Lead technologist Worker

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.

sparse(__DIR__ . '/XmlStructure.xml', array('title'));? css();? 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.

Truncated by Planet PHP, read more at the original (another 2368 bytes)