469,292 Members | 1,294 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,292 developers. It's quick & easy.

ElementTree: namespace declaration in each element?

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?

Nov 19 '06 #1
3 3263
cyberco wrote:
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>

Nov 19 '06 #2
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?

Nov 19 '06 #3
cyberco wrote:
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>

Nov 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by alainpoint | last post: by
3 posts views Thread by Matthew Thorley | last post: by
9 posts views Thread by Chris Spencer | last post: by
8 posts views Thread by Matthew Thorley | last post: by
1 post views Thread by John L. Clark | last post: by
4 posts views Thread by paul.sherwood | last post: by
1 post views Thread by mirandacascade | last post: by
2 posts views Thread by mirandacascade | last post: by
3 posts views Thread by gray.bowman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.