Using ElementTree to tidy up an XML string to my liking

peterbe at gmail.com peterbe at gmail.com
Thu Feb 23 12:37:25 EST 2006


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?




More information about the Python-list mailing list