Making a small change to a large XML document

Gerard Flanagan grflanagan at yahoo.co.uk
Tue Sep 25 08:37:09 CEST 2007


On Sep 25, 12:38 am, Dan Stromberg <dstrombergli... at gmail.com> wrote:
> Say I want to take an existing XML document, and change the value="9997"
> and value="9998" to two different numbers, without changing any of the
> rest of the document - not even changing comments or indentation, if
> avoidable.
>
> What's the best way of doing it in python?
>
>   <bean id="cs" class="c.d.q.s.C">
>     <property name="p" value="9997"/>
>     <property name="mP" value="9998"/>
>     <property name="g" value="cs"/>
>     <property name="e" value="t"/>
>   </bean>
>
> My .xml file is full of such beans.
>
> I've played around with minidom a little in the past, and I'm targetting
> python 2.5.1.
>
> Thanks!

With ElementTree (from xml.etree import ElementTree) and the 'replace'
and 'sub' functions here:

  http://gflanagan.net/site/python/utils/elementfilter/elementfilter.py

(Some improvements to ElementTree:

  http://effbot.org/zone/elementtree-13-intro.htm

)

Gerard




More information about the Python-list mailing list