473,574 Members | 3,123 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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('playl ist.xml')
<snip: adding/removing elements>
tree.write(outF ile)

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:trackLis t>
<ns0:track
<ns0:title>Ki ck 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 3343
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
3724
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 contents of the file are as follows (i hope it will be rendered correctly): <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.0//EN"...
3
4052
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" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.chpc.utah.edu/~baites/mgacML
9
10077
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 like ns0, ns1, etc. The correct behavior should be to write the file in the form that it was read, which it seems to do correctly for everything...
8
5212
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 = et.ElementTree('some_file.xml') xmlb = et.Element('parent') et.SubElement(xmlb, 'child1') et.SubElement(xmlb, 'child2')
1
2717
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 namespace prefix, what is its namespace, if default namespaces do not apply? Are (either of) prefixed or non-prefixed attributes correct? For...
4
4650
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
3055
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
1958
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 the XML document 2) The inherited code is somewhat 'brittle' in that some well-formed XML documents are not correctly processed by the code; the...
3
2080
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 available, gives me ... ElementTree(sectionElement).write("section.xml") TypeError: 'module' object is not callable I'm new to python, so I think...
0
7842
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7764
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8273
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8138
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5658
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5336
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
1
2277
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1375
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1102
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.