XML question, DOM or SAX?

Alan Kennedy alanmk at hotmail.com
Sat Oct 19 14:02:30 CEST 2002

Markus von Ehr:

>> I want to save one or more spectras in a XML file.
>> The program shall be able to read this file into my own
>> data object structure, too.
>> The data can be displayed or altered.
>> After all the data has to be saved again to XML.

Bengt Richter wrote:

> Why XML? Is there another consumer of the data whose requirements
> you are catering to? Have you looked at the pickle module?

I have to agree with Bengt here. Is there a good reason why you want to
use XML for this data? 

You have rejected the main two representation solutions that are
possible, i.e.

 o Putting each datum in an element, thus incurring a large "tag"
 o Putting data series in an attribute, thus requiring parsing

Both would make your data more human readable. If you don't want to make
your data human readable, there is no benefit to using XML. You might as
well use a CSV file, or better still, a relational database.

Maybe it's "documentation" that you're trying to get at? Maybe you want
the format to be self-documenting? In that case, use the original format
you suggested in your original post, i.e.


And put an xml comment in which explains what the data mean. Something

Each spec element contains a series of wavelength measurements, one per


> (BTW, I think someone has also written a pickler that uses XML, but I
> forgot what it's called)

David Mertz has an XML pickler in his Gnosis XML utilities.


>> Shall I use SAX or DOM?

Should you use SAX or DOM for what?

1. For creating the data file from source data (use print statements)

2. To create an in-memory DOM tree from the data (either feed SAX events
to a DOM creator, or use DOM calls directly yourself to construct the
tree. The DOM construction calls will happen either way, so the
efficiency  of each is comparable. It's all down to which API your more
comfortable with)

3. To create a generic in-memory data structure? Do you want to use a
DOM tree so that you don't need to write your own specialised data
structure? If yes, then, please also be prepared for the following

 o Massive memory consumption
 o Massive cpu time consumption

This is, I think, one of those cases where you need to really think
about whether XML is the right data format for you. Why? What benefits
does it offer?


alan kennedy
check http headers here: http://xhaus.com/headers
email alan:              http://xhaus.com/mailto/alan

More information about the Python-list mailing list