Editing XML tags for Time Period tab in Metadata Editor

358
2
08-02-2011 12:14 PM
MikeMacRae
Occasional Contributor III
I'm looking to edit some XML's to upload into some feature classes as metadata. I'm having a little trouble trying to edit/add tags to update the Time Period tab in the ArcCatalogue metadata editor (9.3) using python and the elementtree module.

In this tab, there are 3 options for date. Single date, multiple dates and a range of dates. I want to set a parameter so the user can choose between these 3 options and add dates. I have the range of dates figured out, but the multiple dates I am having trouble with. I'm thinking I need to find and replace the tags I want, but I'm not sure how to accomplish this. I've tried a number of codes I found, but nothing seems to work.

My code goes a little like this:

    folderPath = "Z:\ESRI\Figure_Sourcing\Figures\Metadata\IOR_Run_Metadata_2009"

    for filename in glob.glob(os.path.join(folderPath, "*.xml")):

        fullpath = os.path.join(folderPath, filename)
        
        if os.path.isfile(fullpath):
            basename, filename2 = os.path.split(fullpath)

            root = ElementTree(file=r"Z:\ESRI\Figure_Sourcing\Figures\Metadata\IOR_Run_Metadata_2009\\" + filename2)

            iter = root.getiterator()
            #Iterate
            for element in iter:
                print element.tag

                if element.tag == "begdate":
                    element.tag.replace("begdate", "sngdate")



My XML looks like this. I want to replace the rngdate, begdate and enddate tags so that it looks like the XML at the bottom of this post.

[HTML]
<metadata>
  <idinfo>
    <citation>
      <citeinfo>
          <origin>My Company Name</origin>
          <pubdate>05/04/2009</pubdate>
          <title>Feature Class Name</title>
          <edition>0</edition>
          <geoform>vector digital data</geoform>
          <onlink>.</onlink>
       </citeinfo>
     </citation>
   <descript>
      <abstract>This dataset represents the GPS location of inspection points collected in the field for the Site Name</abstract>
      <purpose>This dataset was created to accompany the clients Assessment Plan. This point feature class represents the location within the area that the field crews collected related data.</purpose>
    </descript>
<timeperd>
   <timeinfo>
     <rngdates>
        <begdate>7/13/2010</begdate>
        <begtime>unknown</begtime>
        <enddate>7/15/2010</enddate>
        <endtime>unknown</endtime>
     </rngdates>
   </timeinfo>
   <current>ground condition</current>
</timeperd>
[/HTML]






[HTML]
<metadata>
  <idinfo>
    <citation>
      <citeinfo>
          <origin>My Company Name</origin>
          <pubdate>05/04/2009</pubdate>
          <title>Feature Class Name</title>
          <edition>0</edition>
          <geoform>vector digital data</geoform>
          <onlink>.</onlink>
       </citeinfo>
     </citation>
   <descript>
      <abstract>This dataset represents the GPS location of inspection points collected in the field for the Site Name</abstract>
      <purpose>This dataset was created to accompany the clients Assessment Plan. This point feature class represents the location within the area that the field crews collected related data.</purpose>
    </descript>
<timeperd>
   <timeinfo>
     <mdattim>
       <sngdate>
            <caldate>08-24-2009</caldate>
            <time>unknown</time>
        </sngdate>
      <sngdate>
            <caldate>08-26-2009</caldate>
        </sngdate>
      <sngdate>
            <caldate>08-26-2009</caldate>
        </sngdate>
      <sngdate>
            <caldate>07-07-2010</caldate>
        </sngdate>
     </mdattim>
   </timeinfo>
   <current>ground condition</current>
</timeperd>
[/HTML]
Tags (2)
0 Kudos
2 Replies
MikeMacRae
Occasional Contributor III
Anyone? 🙂
0 Kudos
MikeMacRae
Occasional Contributor III
I believe I solved this problem. Although it took some time, the answer turned out to be fairly simple so I thought I'd share for those who are interested.

These XML's are in FGDC standard. The code will allow you to edit the XML so that when you import the XML as metadata into a FC, it will change the format in the ArcCatalogue metadata editor, Time Period tab(9.3) from a range of dates to multiple dates. You can also change it to single dates, but you will need to export a FC's metadata that contains single dates to see how the tabs are set up and then you can edit the code accordingly.

If you want to be ambitious, you can set python code to create a whole XML document that populates metadata which you can upload into feature classes. Great for batch processing metadata!

The code that worked for me is as follows:



    # Set workspace location for XML files
    folderPath = "Z:\ESRI\Figure_Sourcing\Figures\Metadata\IOR_Run_Metadata_2009"
    # Loop through each file and search for files with .xml extension
    for filename in glob.glob(os.path.join(folderPath, "*.xml")):

        fullpath = os.path.join(folderPath, filename)
        
        # Split file name from the directory path
        if os.path.isfile(fullpath):
            basename, filename2 = os.path.split(fullpath)
            # Set variable to XML files
            root = ElementTree(file=r"Z:\ESRI\Figure_Sourcing\Figures\Metadata\IOR_Run_Metadata_2009\\" + filename2)
            
            # Set variable for iterator
            iter = root.getiterator()
            #Iterate through the tags in each XML file
            for element in iter:
                if element.tag == "timeinfo":
                    tree = root.find(".//timeinfo")
                    # Clear all tags below the "timeinfo" tag
                    tree.clear()
                    # Append new Element
                    element.append(ET.Element("mdattim"))
                    # Create SubElements to the parent tag
                    child1 = ET.SubElement(tree, "sngdate")
                    child2 = ET.SubElement(child1, "caldate")
                    child3 = ET.SubElement(child1, "time")
                    # Set text values for tags
                    child2.text = "08-24-2009"
                    child3.text = "unknown"
0 Kudos