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() -- http://mail.python.org/mailman/listinfo/python-list