472,368 Members | 2,451 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,368 software developers and data experts.

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 3315
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: alainpoint | last post by:
Hello, I use Elementtree to parse an elementary SVG file (in fact, it is one of the examples in the "SVG essentials" book). More precisely, it is the fig0201.svg file in the second chapter. The...
3
by: Matthew Thorley | last post by:
Why does ElementTree.parse convert my xsi to an xmlns? When I do this from elementtree import ElementTree # Sample xml mgac =""" <mgac xmlns="http://www.chpc.utah.edu/~baites/mgacML"...
9
by: Chris Spencer | last post by:
Does anyone know how to make ElementTree preserve namespace prefixes in parsed xml files? The default behavior is to strip a document of all prefixes and then replace them autogenerated prefixes...
8
by: Matthew Thorley | last post by:
Greetings, perhaps someone can explain this. I get to different styles of formatting for xmla and xmlb when I do the following: from elementtree import ElementTree as et xmla =...
1
by: John L. Clark | last post by:
I am curious as to the rationale, and effect, of having default namespaces not applying (directly) to attributes (see http://www.w3.org/TR/REC-xml-names/#defaulting). Given an attribute without a...
4
by: paul.sherwood | last post by:
Hi Im trying to parse a large(150MB) xml file in order to extract specific required records. import sys from elementtree.ElementTree import ElementTree root = ElementTree(file=big.xml')
1
by: mirandacascade | last post by:
O/S: Windows 2K Vsn of Python: 2.4 Currently: 1) Folder structure: \workarea\ <- ElementTree files reside here \xml\ \dom\
2
by: mirandacascade | last post by:
Situation is this: 1) I have inherited some python code that accepts a string object, the contents of which is an XML document, and produces a data structure that represents some of the content of...
3
by: gray.bowman | last post by:
I'm messing around with trying to write an xml file using xml.etree.ElementTree. All the examples on the internet show the use of ElementTree.write(), although when I try to use it it's not...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.