import arcpy, sys from xml.etree.ElementTree import ElementTree from arcpy import env env.overwriteOutput = True env.workspace = r"D:\GIS\GDB\Lakeland\Lakeland.gdb\AddPoints" # set the workspace for use in ListFeatureClasses fcList = arcpy.ListFeatureClasses() # lists all feature classes from inside env.workspace translatorpath = r"C:\Program Files (x86)\ArcGIS\Desktop10.0\Metadata\Translator\ARCGIS2FGDC.xml" # the translator file to use xmlfile = r"D:\GIS\metadata\test.xml" # the output xml file for fc in fcList: arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) # export the current fc's metadata to xml tree = ElementTree() # make an ElementTree object tree.parse(xmlfile) # read the xml into the ElementTree spot = tree.find("idinfo/descript/abstract") # find whatever tag you want print spot.text # print the text between the tags
I tried today to use the XSLTransform_conversion tool to just extract out everything (avoiding a transform) but I am getting an error message that appears to be a bug in tool validation (which I sent to ESRI support).
import sys, os, arcpy def getMetadataText(Dataset,Element="dataIdInfo/idPurp"): """Extracts metadata contents from ArcGIS metadata arguments Dataset - Path to an ArcGIS dataset Element - XML path in ArcGIS metadata. Default: "metadata/dataIDInfo/idPurp" ("Summary") """ from xml.etree.ElementTree import ElementTree try: # Get arcgis to fgdc translator path ARCGISHOME = arcpy.GetInstallInfo("desktop")["InstallDir"] XSLT = os.path.normpath(os.path.join( ARCGISHOME, "Metadata/Stylesheets/gpTools/exact copy of.xslt")) xmlTemp = os.path.join(os.environ["TEMP"],"xx_xmltemp.xml") # export the metadata to xml arcpy.env.overwriteOutput = True arcpy.XSLTransform_conversion(Dataset,XSLT,xmlTemp) # parse out Description element from ArcGIS 10 metadata tree = ElementTree() # make an ElementTree object tree.parse(xmlTemp) # read the xml into the ElementTree Data = tree.find(Element) # find whatever tag you want del tree arcpy.Delete_management(xmlTemp) return Data.text # return the contents as text except Exception, xmsg: raise Exception, str(xmsg) # You could put a loop here around this to print out Summary for a list of datasets dataset = r"E:\work\work.gdb\BeetleSprayArea.shp" text = getMetadataText(dataset) arcpy.AddMessage(text)
import arcpy, sys from xml.etree.ElementTree import ElementTree from arcpy import env env.overwriteOutput = True env.workspace = r"D:\Lakeland_GIS\GDB\Lakeland\Lakeland.gdb\AddPoints" AGSHOME = arcpy.GetInstallInfo("Desktop")["InstallDir"] translatorpath = AGSHOME + r"Metadata\Translator\ARCGIS2FGDC.xml" xmlfile = r"U:\my docs\GIS Projects\Python\Scripts\List Metadata\working_test.xml" # list any standalone feature classes fcList = arcpy.ListFeatureClasses() fcList.sort() for fc in fcList: arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) tree = ElementTree() tree.parse(xmlfile) spot = tree.find("idinfo/descript/purpose") print fc print spot.text # list feature datasets datasetList = arcpy.ListDatasets() datasetList.sort() for dataset in datasetList: print dataset # list feature classes inside the dataset fcList = arcpy.ListFeatureClasses("","",dataset) fcList.sort() for fc in fcList: arcpy.ExportMetadata_conversion(fc, translatorpath, xmlfile) tree = ElementTree() tree.parse(xmlfile) spot = tree.find("idinfo/descript/purpose") print " " + fc print " " + spot.text