ElementTreeのtostringを使うとnamespaceのprefixがns0, ns1...となって残念な感じなので、変更する方法を調べてた。
register_namespaceで登録しておけばいいらしい。Python2.7.1で確認。
デフォルト
>>> from xml.etree import ElementTree >>> ElementTree.tostring(ElementTree.fromstring('<feed xmlns="http://example.com/"/>')) '<ns0:feed xmlns:ns0="http://example.com/" />'
prefixなし
>>> from xml.etree import ElementTree >>> ElementTree.register_namespace('', 'http://example.com/') >>> ElementTree.tostring(ElementTree.fromstring('<feed xmlns="http://example.com/"/>')) '<feed xmlns="http://example.com/" />'
prefixあり
>>> from xml.etree import ElementTree >>> ElementTree.register_namespace('my', 'http://example.com/') >>> ElementTree.tostring(ElementTree.fromstring('<feed xmlns="http://example.com/"/>')) '<my:feed xmlns:my="http://example.com/" />'
追記
Python2.5、2.6のElementTreeにはregister_namespaceがない。
ElementTree._namespace_mapの辞書で管理されているので、これを書き換えれば対応できる。