By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,985 Members | 1,574 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,985 IT Pros & Developers. It's quick & easy.

ElementTree: namespace declaration in each element?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.