ElementTree: namespace declaration in each element?

C

cyberco

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?
 
F

Fredrik Lundh

cyberco said:
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?

written by a "it's the bytes, not the data" guy, obviously ;-)

the standard serializer in 1.2 doesn't support default namespaces, so
the quickest way to do this is to loop over all tags (use getiterator),
strip off the "{http://xspf.org/ns/0/}" prefix, add an xmlns attribute
to the root (if necessary), and write it out.

</F>
 
C

cyberco

written by a "it's the bytes, not the data" guy, obviously ;-)

hehehe...I figured it was. Wonder 'who' it was... ;)
the standard serializer in 1.2 doesn't support default namespaces, so
the quickest way to do this is to loop over all tags (use getiterator),
strip off the "{http://xspf.org/ns/0/}" prefix, add an xmlns attribute
to the root (if necessary), and write it out.

OK, that's what I was already doing. Thanks.
Will the standard serializer in 1.3 support default namespaces?
 
F

Fredrik Lundh

cyberco said:
OK, that's what I was already doing. Thanks.
Will the standard serializer in 1.3 support default namespaces?

that's the plan. I've been working on a more modular XML writer, which
will make it easier to tweak the output in various ways, without having
to write everything from scratch.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top