<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: One way to update metadata using Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/one-way-to-update-metadata-using-python/m-p/471481#M36795</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Very Helpful - Thanks for posting. If you figure out how to do this in ArcGIS 10 please feel free to post. Thanks again,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Tracy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 18 Jan 2013 17:22:08 GMT</pubDate>
    <dc:creator>TracyJones</dc:creator>
    <dc:date>2013-01-18T17:22:08Z</dc:date>
    <item>
      <title>One way to update metadata using Python</title>
      <link>https://community.esri.com/t5/python-questions/one-way-to-update-metadata-using-python/m-p/471479#M36793</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;For those interested in using Python to update metadata I have figured out one way to do it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The big trick is that this can't be done using the ESRI Python tools such as arcpy as those tools block access to the metadata fields.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;My environment is SDE on Oracle.&amp;nbsp; Adjust this script as appropriate for your environment.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;
# SDMetadataChangeBasic.py
# 2/24/2012&amp;nbsp; David Anderson&amp;nbsp; Bighorn National Forest
# A python script to manipulate metadata values stored in SDE
# I run this using Python 2.6 that comes withArcGIS 10.0.&amp;nbsp; Running against a SDE version 9.3 installed onOracle 11.

# Import in the various modules that weill be used
import cx_Oracle
from xml.etree import ElementTree
import StringIO

# Establish a connection to the database.&amp;nbsp; For this script Oracle is the database storing the SDE data
ora_conn = cx_Oracle.connect('[s_r02_bnf]/@idb')&amp;nbsp; # privileged admin account that can edit metadata
ora_curs=ora_conn.cursor()

# SQL query to get metadata records to manipulate.&amp;nbsp; Note the for update clause at the end.&amp;nbsp; This is important to include
sql = "select xml from g_r02_bnf.gdb_usermetadata where name='Cultural_Districts' for update"
# Execute the query and pull the data into a Python variable
xml_cur=ora_curs.execute(sql)
xml_val=xml_cur.fetchone()

# Straightforward until here.
# Now comes a little bit of manipulation
# The goal is to get the XML from the SDe table into a ElementTree so it can be easily manipulated.
# ElementTrees are parsed from files, so need to make the table value look like a file.
# Luckily Python as a module for that.&amp;nbsp; StringIO
xml_string_file=StringIO.StringIO(xml_val[0])

# Create an XML tree variable
xml_tree = ElementTree.ElementTree()

# And put some data into the variable
xml_tree.parse(xml_string_file)

# WooHoo.&amp;nbsp; At this point we can now manipulate the metadata xml using the python xml module tools

# Lets go to some element to change
# Knowing the XML tags and the order to get to them requires knowledge about the metadata xml format.
# On option is to print the xml value (print xml_val[0]), copy and paste into a text file, save the file, then open that file with a browser such a firefox
# I use the XML browser tool in PLSQL developer from Oracle.
purpose_element = xml_tree.find("idinfo/descript/purpose")

# Change the value to whatever you want
purpose_element.text='A new purpose, a new hope.'

# Now time to reverse the process to put the updated XML back into the metadata table
# Need a place to put the updated xml.
# Create a new in memory file using the handy dandy StringIO module
xml_output_file=StringIO.StringIO()&amp;nbsp; #

# Use the ElementTree functionality to write out the updated xml
# The write command take a file as input, which is why use stringio to create something that looks like a file.
# Could actually do this with a real file.&amp;nbsp; Keeping all in memory seems cleaner and faster.
xml_tree.write(xml_output_file)

# Head back to the top of the file
xml_output_file.seek(0)

# Now read the data into a simple string variable
my_new_xml = xml_output_file.read()

# The above proces seems rather convulted process of converting.&amp;nbsp; But it works.

# One thing the write function does not seem to do is put in the correct header information so that the XML can be interpreted as XML.
# This is copied straight from the ESRI Metadata XML header
add_string='&amp;lt;?xml version="1.0"?&amp;gt;&amp;lt;!--&amp;lt;!DOCTYPE metadata SYSTEM "http://www.esri.com/metadata/esriprof80.dtd"&amp;gt;--&amp;gt;'
my_newer_xml = add_string + my_new_xml

# Now use the cx_Oracle functionality to write the data back into Oracle.
# To make this happen is why the for update clause was added to the select.&amp;nbsp; The update clause lets Oracle put a lock on the record.
# One quick check.&amp;nbsp; If the new xml is smaller than the original the Oracle LOB has to shrunk
if len(my_newer_xml) &amp;lt; xml_val[0].size():
&amp;nbsp;&amp;nbsp; xml_val[0].trim(len(my_newer_xml))

xml_val[0].write(my_newer_xml)

# Oracle is transactional so the change has to be commited.
ora_conn.commit()

# Done
# It seems so easy and logical afterwards.
&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 24 Feb 2012 16:54:39 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/one-way-to-update-metadata-using-python/m-p/471479#M36793</guid>
      <dc:creator>David_JAnderson</dc:creator>
      <dc:date>2012-02-24T16:54:39Z</dc:date>
    </item>
    <item>
      <title>Re: One way to update metadata using Python</title>
      <link>https://community.esri.com/t5/python-questions/one-way-to-update-metadata-using-python/m-p/471480#M36794</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;David, &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for posting this.&amp;nbsp; It was very helpful.&amp;nbsp; We been jumping through a lot of hoop to manage our Oracle SDE metadata and I think this will definitely simplify the process for us.&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Cheers!&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Bill&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 15 Jan 2013 21:43:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/one-way-to-update-metadata-using-python/m-p/471480#M36794</guid>
      <dc:creator>BillDaigle</dc:creator>
      <dc:date>2013-01-15T21:43:58Z</dc:date>
    </item>
    <item>
      <title>Re: One way to update metadata using Python</title>
      <link>https://community.esri.com/t5/python-questions/one-way-to-update-metadata-using-python/m-p/471481#M36795</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Very Helpful - Thanks for posting. If you figure out how to do this in ArcGIS 10 please feel free to post. Thanks again,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Tracy&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 18 Jan 2013 17:22:08 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/one-way-to-update-metadata-using-python/m-p/471481#M36795</guid>
      <dc:creator>TracyJones</dc:creator>
      <dc:date>2013-01-18T17:22:08Z</dc:date>
    </item>
  </channel>
</rss>

