LL2XML - A Python module to convert list of lists to XML, or an HTML table, if you prefer.    
         
         
    Get LL2XML    
   

LL2XML 0.5.1 (posted 2 August 2002)

*View source* and save as "LL2XML.py" into a suitable folder, ideally your Python/Lib folder, created when you install Python.

(Why view source? Some web browsers do an HTML escape when reading a .txt file. You can always right-click the above link and Save target as or Copy link location to avoid any problems.)

A note for people using the version of this module in the Python Cookbook can be found at the foot of this page.

Standard disclaimer:
Please note that you use LL2XML at your own risk. If in doubt, read the source.

News
29 August 2002
Doug Hoppe has sent me a new version of the LL2XML function of this module which has some additional features (for example, the ability to specify list items which should be excluded from the XML output).

I'm posting it here in the first instance so it is available for people to evaluate until I have time to look at integrating the code with the main version.

It looks like Doug mostly uses ASV to process his source files before passing the data to LL2XML, in case you are wondering what the references to ASV are in his code. His version improves compatability with the ASV output. (I must install ASV and give it a try).

It would be great if people could send me any comments.

   
         
         
   

The script requires Python to be installed on your computer. If you don't have Python, go to www.python.org to download it.

 

   
   

Using LL2XML

This module converts a list of lists into xml.
With the proper arguments, the XML output will be an HTML table.
(See the test function for examples.)

If you want to use a csv as input, you will first need to get
hold of a csv parser to create the list of lists.
Examples include those at:
http://tratt.net/laurie/python/asv/
and
http://www.object-craft.com.au/projects/csv/

The function LL2XML in this module accepts up to five arguments:
1) a list of lists, each sublist having the same number of elements (required)
2) a tuple of heading values (optional - see below for default behaviour), same length as the sublists
3) a root element name (optional - default is "rows")
4) a row element name (optional - default is "row")
5) a "yes" or "no" string controlling if the XML declaration is included (optional - default is "yes").

If no header tuple is supplied the first sublist is taken to be a list of
headers,and its values used to create the element names for the XML rendition
of the other sublists.

If you need to supply the XML element names, add them as the first sublist or
use the optional headings tuple argument.

Element names (This paragraph updated 13 July 2002)
Headings are normalised by the script to string type, lower case and have any
spaces replaced by underscores. The current version of LL2XML does not do any
element name validation. You should check that your proposed headings are XML compliant.
See this article for more information on XML element names.
I plan to add some degree of element name validation in version 0.6 of the script.

Using the root_element and row_element arguments
Example: If your input is a list of lists containing employee details, you may
want to use "employees" as the root element name, and "employee" as the
row level element name. (See example 2 in the module to see one way to
do this.)

Note that "<",">" and "</" should not be used to wrap these names.
Just define them in plain text. If these two arguments are not defined
in the function call, the defaults - "rows" and "row" are used.
See the examples in the test function below for more detail.

Headers and content are converted to string representations if they are not
already strings. Certain characters that have meaning in XML are replaced by
standard XML entities in the output:
< becomes &amp;
' becomes &apos;
< becomes &lt;
> becomes &gt;
" becomes &quot;

Using LL2XML
To use LL2XML in another module:
import LL2XML
LL = *whatever your list of lists is*
xml = LL2XML.LL2XML(LL)
print xml

To test:
import LL2XML
LL2XML.test()

See the test function in the script for more on how to use it.

What's new in version 0.5.1: A tidy up. The backtick `s` string conversion now uses str(s), a redundant function call is removed, and literal empty strings are used to check against empty headings. The example list of lists with no headings is now derived from the one with headings (D'oh!).

What was new in LL2XML 0.5: Now headings are properly escaped, and the digit 0 is allowed as a heading.

What was new in version 0.4:

  • I fixed a bug which made the heading list length exception not work properly.
  • I've used list comprehensions to check that all of the sublists are of equal length. This means that the module requires Python 2.0 or above.
  • The string module is not imported, as string methods are used throughout.

Version 0.3.1 is still available for those who need to use earlier versions of Python. It's a bug fixed version of 0.3, with all of the fixes present in 0.4, but list comprehensions and string methods are not used. Please let me know if there is anything I've missed in making this Python 1.5.2 compliant.

Please send comments and suggestions to LL2XML at outwardlynormal dot com

Many thanks to Dave Cole for the optimisation suggestions.

I hope people find this useful. Please do let me know if you use it and if you have any comments or suggestions.

Note for people using the O'Reilly version of this module

An edited version of this module appears in the Python Cookbook. Unfortunately the hard copy version has three errors, introduced during the editing/publishing process.

Lines 30 and 31 in the O'Reilly hard copy version are:

s = s.replace("<", "<")
s = s.replace(">", ">")

they should be

s = s.replace("<", "&lt;")
s = s.replace(">", "&gt;")

The other entity replacements seem to have gone through unscathed.

Line 37

s = 's'

This should be

s = `s`

or, better,

s = str(s)

As of 23 August 2002, these have been corrected in the code download on the O'Reilly site, so their version should now work fine.

 

 

   
         
    <BACK TO PYTHON PAGE>      <SQUIDGE>       <OVINE>       <HOME>