Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find and Replace Values in XML using Python

I am looking to edit XML files using python. I want to find and replace keywords in the tags. In the past, a co-worker had set up template XML files and used a "find and replace" program to replace these key words. I want to use python to find and replace these key words with values. I have been teaching myself the Elementtree module, but I am having trouble trying to do a find and replace. I have attached a snid-bit of my XML file. You will seen some variables surrounded by % (ie %SITEDESCR%) These are the words I want to replace and then save the XML to a new file. Any help or suggestions would be great.

Thanks, Mike

<metadata> <idinfo> <citation> <citeinfo>  <origin>My Company</origin>  <pubdate>05/04/2009</pubdate>  <title>POLYGONS</title>  <geoform>vector digital data</geoform>  <onlink>\\C$\ArcGISDevelopment\Geodatabase\PDA_STD_05_25_2009.gdb</onlink> </citeinfo> </citation>  <descript>  <abstract>This dataset represents the mapped polygons developed from the field data for the %SITEDESCR%.</abstract>  <purpose>This dataset was created to accompany some stuff.</purpose>  </descript> <timeperd> <timeinfo> <rngdates>  <begdate>%begdate%</begdate>  <begtime>unknown</begtime>  <enddate>%enddate%</enddate>  <endtime>unknown</endtime>  </rngdates>  </timeinfo>  <current>ground condition</current>  </timeperd> 
like image 422
Mike Avatar asked Jun 29 '11 16:06

Mike


People also ask

Can you use XML with Python?

Python enables you to parse and modify XML documents. In order to parse XML document, you need to have the entire XML document in memory.

How to parse XML in python ElementTree?

There are two ways to parse the file using 'ElementTree' module. The first is by using the parse() function and the second is fromstring() function. The parse () function parses XML document which is supplied as a file whereas, fromstring parses XML when supplied as a string i.e within triple quotes.

Which python library is used to transform XML data structure into Element tree structure?

ElementTree is an important Python library that allows you to parse and navigate an XML document. Using ElementTree breaks down the XML document in a tree structure that is easy to work with.


2 Answers

The basics:

from xml.etree import ElementTree as et tree = et.parse(datafile) tree.find('idinfo/timeperd/timeinfo/rngdates/begdate').text = '1/1/2011' tree.find('idinfo/timeperd/timeinfo/rngdates/enddate').text = '1/1/2011' tree.write(datafile) 

You can shorten the path if the tag name is unique. This syntax finds the first node at any depth level in the tree.

tree.find('.//begdate').text = '1/1/2011' tree.find('.//enddate').text = '1/1/2011' 

Also, read the documentation, esp. the XPath support for locating nodes.

like image 109
Mark Tolonen Avatar answered Sep 23 '22 00:09

Mark Tolonen


If you just want to replace the bits enclosed with %, then this isn't really an XML problem. You can easily do it with regex:

import re xmlstring = open('myxmldocument.xml', 'r').read() substitutions = {'SITEDESCR': 'myvalue', ...} pattern = re.compile(r'%([^%]+)%') xmlstring = re.sub(pattern, lambda m: substitutions[m.group(1)], xmlstring) 
like image 40
Ismail Badawi Avatar answered Sep 20 '22 00:09

Ismail Badawi