ArcGIS 10 SP3

I have read the other posts in this forum about updating metadata programmatically, and have examined Esri's help topics regarding xslt stylesheets to update metadata, but am hoping that someone has produced a more efficient solution in the last ten months:

I would like to perform something like the following examples:

  1. for all layer files which contain the word "road" in their name, add the following list of words to their tags: road, street
  2. for all SDE feature classes which do not currently have an Item Description, add the feature class name and tags into the Item Description etc.

My question is: can I do this with python or VBA, or must I use xml/xslt stylesheets to do this?


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()

#--feature description
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[0]. If you need more I can provide you with some scripts I wrote, though I'll refrain from posting them in their entirety here.

  • 1
    You can also use some XPath queries with ElementTree, allowing you to be explicit with which tags you want to update or not. Note that the version of ElementTree that ships with Python 2.6.5 (the version of Python that ships with ArcGIS 10.0) is less than 1.3 so those features added at 1.3 aren't available. You could also use another XML parser such as lxml. – blah238 Dec 21 '11 at 18:55

I haven't tried this, but if you are looking to edit the metadata XML directly with Python, you could use the ElementTree library (which is part of the standard lib). Not sure what direct editing of the XML would do though, as far as its relationship with its featureclass. Test in a dev environment.

  • If it's a shapefile you can edit its XML metadata directly since it's a discrete file. If it's a geodatabase feature class you would first need to export the metadata to a dummy XML file, edit it, and then import it again, or use ArcObjects to edit it directly. – blah238 Dec 21 '11 at 18:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.