ElementTree does allow one to programatically edit XML metadata files. I've used this on a recent project to update many tags using information stored in a database. So basically for each shapefile I have a data description, citation, abstract, etc. stored in a table and access the tags using
ElementTree and retrieving the metadata using a search cursor. I'm not an expert on the structure of the
ElementTree library, but the long and short of it is that you create an "iterator" object which is the parent tag of the "subelement(s)" you want to edit. Say, for example you have the following tags and you want to change the publisher information:
<publish>U.S. Geological Survey, Reston, VA</publish>
You could write something like the following snippet in python:
import xml.etree.ElementTree as et
#--get xml file and parse it
root = et.parse(os.path.join(shpPath,xmlFile)).getroot()
iterator = root.getiterator('pubinfo')
for elem in iterator:
subelem = 'publish'
old_subelem = elem.find(subelem)
new_subelem = et.SubElement(elem, subelem)
new_subelem.text = 'New Publisher, Anytown, USA'
Note that the iterator object will search the entire XML file looking for the tag "pubinfo". If multiple tags share the same name, the iterator object will contain one element for each occurrence. In this case you will have to dig through your XML files to make sure you are working on the correct one. Say there are 2 instances of the tag
pubinfo (one could be for the "local citation" and one could be for the "larger work citation") and you only want to change the subelement
publish in first one you would replace the
for elem in iterator with
elem = iterator. If you need more I can provide you with some scripts I wrote, though I'll refrain from posting them in their entirety here.