Making a small change to a large XML document

Discussion in 'Python' started by Dan Stromberg, Sep 24, 2007.

  1. 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!
     
    Dan Stromberg, Sep 24, 2007
    #1
    1. Advertising

  2. Dan Stromberg

    Shriphani Guest

    Dan Stromberg 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!


    Hello,
    I hope this works:

    #!/usr/bin/python

    from BeautifulSoup import BeautifulStoneSoup
    #If the XML you provided is in a file named 'problem'
    xml_struc = open('problem','r')
    soup = BeautifulStoneSoup(xml_struc)
    list_of_tags = soup.findAll('property')
    for tag in list_of_tags:
    if tag['value'] == "9998":
    tag['value'] = "9999" #or whatever number you like
    soup_str = str(soup)
    f = open('output', 'w')
    f.write(soup_str)
    f.close()
    xml_struc.close()
     
    Shriphani, Sep 25, 2007
    #2
    1. Advertising

  3. On Sep 25, 12:38 am, Dan Stromberg <> 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
     
    Gerard Flanagan, Sep 25, 2007
    #3
  4. Dan Stromberg

    Paddy Guest

    On Sep 24, 11:38 pm, Dan Stromberg <> 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.
     
    Paddy, Sep 25, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tony Prichard
    Replies:
    0
    Views:
    731
    Tony Prichard
    Dec 12, 2003
  2. Manish Hatwalne
    Replies:
    1
    Views:
    401
    Martin Honnen
    Jul 13, 2004
  3. Henry S. Thompson
    Replies:
    0
    Views:
    391
    Henry S. Thompson
    Sep 7, 2005
  4. Replies:
    0
    Views:
    506
  5. Dag Sunde
    Replies:
    4
    Views:
    357
Loading...

Share This Page