ElementTree.write() question

Fredrik Lundh fredrik at pythonware.com
Fri Dec 17 03:25:22 EST 2004


Stephen Waterbury wrote:

> [If there is a separate list for elementtree, please someone
> clue me ... I didn't see one.]

the xml-sig is preferred, I think.

> Fredrik or other xml / elementtree gurus:
>
> I see from the source that ElementTree.write() writes
>
> <?xml version="1.0"? encoding=[whatever]>
>
> at the beginning of the xml output if an encoding
> other than utf-8 or us-ascii is selected.  Shouldn't
> it also write that if utf-8 or us-ascii is being
> used?  Or at least the
>
> <?xml version="1.0"?>

this is mostly for historical reasons; early versions only supported UTF-8 output,
and never generated encoding directives, so people ended up writing:

    print >>myfile, "<document>"
    for elem in list_of_elements:
        ElementTree(elem).write(myfile)
    print >>myfile, "</document>"

version 1.3 will probably include an option to always write the header.

(note that the XML header isn't needed for UTF-8 and ASCII; if it's not there,
a proper XML parser will treat the stream as UTF-8, and ASCII is a compatible
subset of UTF-8).

</F> 






More information about the Python-list mailing list