Extra XML Support

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

http://blog.anscamobile.com/2011/07/how-to-use-xml-files-in-corona/

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:

1
2
3
<article>
    <title>Some title I thought of</title>
</article>
views:2115 update:2012/2/12 11:34:30
corona forums © 2003-2011