473,386 Members | 1,860 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Using ElementTree to tidy up an XML string to my liking

I have an XML string coming in from one system that I'd like to tidy up
and return in a very particular format. I'm picky!

If the input is
<SOMETHING attr1="foo1"
attr2='foo2' >

Then the output must be
<something
attr1="foo1"
attr2="foo2">

The file might have comments and namespaces and all of this should be
preserved as it was when it came in.

I was hoping to use ElementTree which can parse my input so that I rest
on solid foundations instead of mad regular expressions and string
manipulation.
But, if mad regular expressions and string manipulations is what it
takes I'll have to settle on that.

What are my options?

Feb 23 '06 #1
6 1619
...I hope that you are aware that xml is *case sensitive*

Feb 24 '06 #2
Yes I am but, I'm using a DOM Serializer in Firefox which for some
reason turns myCamelNames into MYCAMELNAMES for the nodenames. I'll
therefore need to control the case-spelling of these things as I'm
formatting the XML string.

Feb 24 '06 #3
pe*****@gmail.com wrote:
Yes I am but, I'm using a DOM Serializer in Firefox which for some
reason turns myCamelNames into MYCAMELNAMES for the nodenames. I'll
therefore need to control the case-spelling of these things as I'm
formatting the XML string.


I realize that it's difficult to make sure that every user of your
system has a corrected Firefox, but it seems to me that there is
a bug in that DOM Serializer, and that it would be good to mend
that.

Concerning element names, it's your coice of course, but I agree
more and more with Guido and PEP008 that camelCase is ugly. (Not
that ALLCAPS is better...)
Feb 24 '06 #4
pe*****@gmail.com wrote:
Yes I am but, I'm using a DOM Serializer in Firefox which for some
reason turns myCamelNames into MYCAMELNAMES for the nodenames. I'll
therefore need to control the case-spelling of these things as I'm
formatting the XML string.


I am almost certain there is something wrong you are doing in Mozilla if
that is happening. My first guess is that you are really doing HTML even
thought you think you are doing XML, and therefore Mozilla converts your
stuff to uppercase. Without seeing the source it is hard to say. I would
advice you write to the Mozilla forums for advice on that.

--
Heikki Toivonen
Feb 24 '06 #5
On Fri, 24 Feb 2006 18:21:59 +0100, Magnus Lycka wrote:
Concerning element names, it's your coice of course, but I agree
more and more with Guido and PEP008 that camelCase is ugly. (Not
that ALLCAPS is better...)


I can see in PEP008 where it says Capitalized_Words_With_Underscores is
ugly, but I can't see where it says pure camelCase is ugly ?

--
Richard
Feb 25 '06 #6
Richard Townsend wrote:
On Fri, 24 Feb 2006 18:21:59 +0100, Magnus Lycka wrote:
Concerning element names, it's your coice of course, but I agree
more and more with Guido and PEP008 that camelCase is ugly. (Not
that ALLCAPS is better...)


I can see in PEP008 where it says Capitalized_Words_With_Underscores is
ugly, but I can't see where it says pure camelCase is ugly ?


Sorry, meant mixedCase (as it was in the XML example). Peter confused
me by writing "myCamelNames"

"""
Function Names

Function names should be lowercase, possibly with words separated by
underscores to improve readability. mixedCase is allowed only in
contexts where that's already the prevailing style (e.g. threading.py),
to retain backwards compatibility.

Method Names and Instance Variables

The story is largely the same as with functions: in general, use
lowercase with words separated by underscores as necessary to improve
readability.
"""
Feb 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: mirandacascade | last post by:
Verion of Python: 2.4 O/S: Windows XP ElementTree resides in the c:\python24\lib\site-packages\elementtree\ folder When a string that does not contain well-formed XML is passed as an argument...
1
by: Gerard Flanagan | last post by:
Pseudo-XPath support for ElementTree with the emphasis on 'Pseudo'. http://gflanagan.net/site/python/pagliacci/ElementFilter.html It's an approach suggested by the Specification Pattern eg....
15
by: Steven Bethard | last post by:
I'm having trouble using elementtree with an XML file that has some gbk-encoded text. (I can't read Chinese, so I'm taking their word for it that it's gbk-encoded.) I always have trouble with...
0
by: Greg Aumann | last post by:
I am trying to write some python code for a library that reads an XML-like language from a file into elementtree data structures. Then I want to be able to read and/or modify the structure and then...
2
by: rajarshi.guha | last post by:
Hi, recently having discovered ElementTree I'm stumped by a very simple problem, which I can't find the answer to. I have some XML in a string object. Now the parse() method of ElementTree takes...
6
by: Sébastien Boisgérault | last post by:
I guess I am doing something wrong ... Any clue ? Traceback (most recent call last): File "<stdin>", line 1, in ? File "/usr/lib/python2.4/site-packages/elementtree/ElementTree.py", line 960,...
2
by: Rick Muller | last post by:
I'm a computational chemist who frequently dabbles in Python. A collaborator sent me a huge XML file that at one point was evidently modified by a now defunct java application. A sample of this...
11
by: Peter Pei | last post by:
One bad design about elementtree is that it has different ways parsing a string and a file, even worse they return different objects: 1) When you parse a file, you can simply call parse, which...
2
by: =?ISO-8859-1?Q?J=2E_Pablo_Fern=E1ndez?= | last post by:
Hello, Is ElementTree supposed to load DTDs? I have some xmls heavy on entities and it fails this way: Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42) on linux2 Type "help", "copyright",...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...

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.