Making a small change to a large XML document

Paddy paddy3118 at googlemail.com
Tue Sep 25 02:59:13 EDT 2007


On Sep 24, 11:38 pm, 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!

If it was machine formatted, regular XML, on a unix box then I'd use
sed:

  sed -e 's/value="9997"/value="zzz"/g;s/value="9998"/value="ZZZ"/g' \
    infile > outfile

It would work for what you said, but if other modifications are
needed
then it is short enough to throw away if necessary and write a larger
script in Python.

- Paddy.




More information about the Python-list mailing list