ElementTree: namespace declaration in each element?

cyberco cyberco at gmail.com
Sun Nov 19 13:23:58 CET 2006


The (excellent) ElementLibrary ads namespaces to each element when
writing the tree back to a file.
Say I do:

tree = ET.parse('playlist.xml')
<snip: adding/removing elements>
tree.write(outFile)

with the following XML:

=============
<playlist version="1" xmlns="http://xspf.org/ns/0/">
    <trackList>
        <track>
            <title>Kick off!</title>
        </track>
    </trackList>
</playlist>
=============

Writing the tree out after adding/removing elements results in:

=============
<ns0:playlist version="1" xmlns:ns0="http://xspf.org/ns/0/">
    <ns0:trackList>
        <ns0:track
            <ns0:title>Kick off!</ns0:title>
        </ns0:track>
    </ns0:trackList>
</ns0:playlist>

=============

Unfortunately the client that needs to consume this XML can't handle
the 'ns0:' part of each tag. Is there a way to output XML similar to
the input?




More information about the Python-list mailing list