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>

with the following XML:

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

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

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


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