Extra XML Support

No replies
User offline. Last seen 4 hours 14 sec ago. Offline
Joined: 17 Aug 2010

If you want to load an XML file there is a great blog post by Jonathan Beebe here:


With XML files which don't have complex elements (like CDATA) the XML library module he uses is nice and fast and produces an easy to navigate table structure. In short, each element is stored in a table, it's element properties are list in key/value pairs in a table called '.properties', the child elements numerically indexed in a table called '.child' and the textual body content, if present, stored in '.value'.

There are just two problems with this library:

1: It does not save changes to the XML table content back to file

2: I prefer to name my variables so that I don't need to index them by number all the time.

To implement solutions to these I decided to extend the library code. First is a 'saveFile' function which takes the original format table (as returned from loadFile) and writes it back to a regular XML file. The one caveat is that you must tell saveFile what the root XML element name is because that element gets represented by the returned table from loadFile.

Second is a pair of functions for converting the original format table into a more DIY format and back again. This means that if your XML contains an element with another single child element, for example:

    <title>Some title I thought of</title>

Then the child is no longer accessed like this:

local thetitle = myxmltable.child[1].value

It is now accessed like this:

local thetitle = myxmltable.title.value

Of course, most XML files will go much deeper than that, which is where the power lies. If you are used to using XPath this will be a more natural form, too.

With the new 'saveFile', 'simplify' and 'desimplify' functions you can now load an XML file, access it's values naturally, update it (even adding new structures and properties) and save it again.

It bears repeating, however, that the table returned by the loadFile function directly represents the root XML element. Therefore, it's element name is accessed by:

local thetitle = myxmltable.name

Using the new 'simplify' function loses this value because element names are now used as the table names. The properties were previously stored in 'properties' under each child table but are now directly accessed by key index. Therefore, the root element would have incorrectly become a property and so is lost. To support this, the new function all take a parameter to name the root element.

This is explained with sample code and a new 'deep' dump() function in this Code Exchange post: