How to generate XML

Irmen de Jong usenet at NOSPAM-irmen.cjb.net
Sat Dec 22 08:15:56 EST 2001


"Martin von Loewis" <loewis at informatik.hu-berlin.de> wrote in message
news:j44rmko5s6.fsf at informatik.hu-berlin.de...

> I'm amazed that people always start proposing difficult-to-use
> libraries when this question comes up. I'd prefer to use
>
> print "<foo/>"


I've created various applications that parse XML using a parser, and
generate XML using plain print statements.
The generated XML depended on the data set, i.e. it wasn't a static
document.
Also the size varied from about one printed page to more than 5 printed
pages, nothing too spectacular.

But every single time there were various problems:
1. syntax errors (like not escaping <, or forgetting a /)
2. structural errors (opening tags were not closed, wrong nesting)
3. inconsistencies (one ducument had US-ASCII encoding, the other no
specified encoding, the next
    had \n\r line separators, another didn't have any line separators)
4. difficulties when changing specs. Restructuring the XML usually meant:
erase and type it all in once again.
5. Creating recursive or repeated structures is somewhat awkward, and the
resulting XML is almost never
    looking "good" for the human eye; usually it's not indented at all.

If I need to create a XML doc that is larger than, say, 10 elements, I will
*not* do that using prints.
I will use DOM, or some other API, to create it in a structured, consistent
way.
I believe that all five problems above are solved by doing it this way.

What would be wrong with (hypothetical code):-
    doc=Doc("UTF-8")
    root=Element("root",{"type","sometype"})
    root.add(Element("subelt",  "the contents"))
    root.add(Element("empty"))
    root.add(...some other document root element...)   # great for
recursive/repeating structures
    doc.add(root)
    print doc.XML()

and then getting
<?xml version="1.0" encoding="UTF-8">
<root type="something">
    <subelt>The contents</subelt>
    <empty/>
    .... here is some other document included....
</root>


Just my ?0.02.

Regards
Irmen de Jong






More information about the Python-list mailing list