XML help needed

Martin von Loewis loewis at informatik.hu-berlin.de
Mon Nov 26 10:46:21 CET 2001

"Duncan Smith" <buzzard at urubu.freeserve.co.uk> writes:

> Thanks for the reply.  Basically I have an XML file containing data
> that I need as various Python objects (strings, lists, arrays etc.).
> I don't need to make changes to XML files, although I might want to
> write data to new XML files of the same type (same DTD).

I see. In that case, I recommend to use plain print statements to
generate the XML (or other means of string processing, like collecting
pieces of the document in a list). This is easier than generating a
DOM or SAX in-memory representation, and then serializing it.

> (I don't really want to have to learn about XSL, XSLT, X-path, SAX,
> DOM, Pyxie etc. unless I know it's going to be useful.)

Depending on the exact processing job, XSLT may be indeed useful. If
you know that the output only depends on the input, then writing a
style sheet that does the transformation, without writing a line of
Python, might be possible.

> Learning all that lot would be a lot of effort to achieve what I
> need.  So if you (or anyone) could give me advice along the lines of
> "DOM would be appropriate but you'll have to know... as well", then
> I'd be grateful.

DOM alone will allow you to get all information from the document, in
a moderatly convenient way. If you find to that you write algorithms
of the kind

Starting from the root, go to the first child element "foo", then to
the last child element "bar", and take its "foobar" attribute value

than XPath would help to tighten your code.

> Martin, you were bang on with the 'invalid XML' thing.  Cheers.

That's a phrase (to be bang on) that is beyond my English
understanding. I take it to mean something good, in absence of a clear
understanding :-)


More information about the Python-list mailing list