<?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: How to replace data in service definition draft file using python? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131637#M63440</link>
    <description>&lt;P&gt;Haven't tried this, but maybe you could treat this just as a text file and manipulate the tags using simple text reading, extracting/deleting the desired block, appending the replacement block, then appending the remaining xml? Or instead of replace, do a remove and insert?&lt;/P&gt;</description>
    <pubDate>Fri, 07 Jan 2022 16:34:07 GMT</pubDate>
    <dc:creator>Anonymous User</dc:creator>
    <dc:date>2022-01-07T16:34:07Z</dc:date>
    <item>
      <title>How to replace data in service definition draft file using python?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131124#M63413</link>
      <description>&lt;P&gt;i have a service definition draft file which also contains the information about the metadata it's going to create. i want to update the metadata information before staging the draft file. The draft file looks something like this :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;SVCManifest xmlns:xs="http://www.w3.org/2001/XMLSchema"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:type="typens:SVCManifest"&amp;gt;
&amp;lt;Databases xsi:type="typens:ArrayOfSVCDatabase" /&amp;gt;
&amp;lt;Resources xsi:type="typens:ArrayOfSVCResource"&amp;gt;
&amp;lt;SVCResource xsi:type="typens:SVCResource"&amp;gt;
&amp;lt;ID&amp;gt;{429221BF-D0A1-40D8-9DC1-B41D269E95C7}&amp;lt;/ID&amp;gt;
&amp;lt;Name&amp;gt;test.crf&amp;lt;/Name&amp;gt;
&amp;lt;Metadata xsi:type="typens:XmlPropertySet"&amp;gt;
&amp;lt;XmlDoc&amp;gt;&amp;amp;lt;?xml version="1.0"?&amp;amp;gt;
&amp;amp;lt;metadata xml:lang="en"&amp;amp;gt;&amp;amp;lt;Esri&amp;amp;gt;&amp;amp;lt;CreaDate&amp;amp;gt;20211219&amp;amp;lt;/metadata&amp;amp;gt;
&amp;lt;/XmlDoc&amp;gt;
&amp;lt;/Metadata&amp;gt;
&amp;lt;/SVCManifest&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i am able to read the XmlDoc tag in this file using the code below and i want to replace the xml file under this tag with another xml file. This code helps me read it.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import xml.dom.minidom as DOM
import shutil
import xml.etree.ElementTree as ET
metadata_path=r"C:\Users\arcgis\UnidataDD2MI_result.xml"
meta=doc.getElementsByTagName('XmlDoc')
new_metadata=DOM.parse(metadata_path)
for metadata in meta:
    if metadata.firstChild.data:
        metadata.firstChild.replaceData()
        print (metadata.firstChild.data)
sys.exit(0)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the file i want to have under the tag:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;&amp;lt;?xml version="1.0" encoding="UTF-8"?&amp;gt;
&amp;lt;gmd:MD_Metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:gco="http://www.isotc211.org/2005/gco"
                 xmlns:gmd="http://www.isotc211.org/2005/gmd"
                 xmlns:srv="http://www.isotc211.org/2005/srv"
                 xmlns:gmx="http://www.isotc211.org/2005/gmx"
                 xmlns:gsr="http://www.isotc211.org/2005/gsr"
                 xmlns:gss="http://www.isotc211.org/2005/gss"
                 xmlns:gts="http://www.isotc211.org/2005/gts"
                 xmlns:gml="http://www.opengis.net/gml/3.2"
                 xmlns:xlink="http://www.w3.org/1999/xlink"
                 xmlns:xs="http://www.w3.org/2001/XMLSchema"
                 xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/csw/2.0.2/profiles/apiso/1.0.0/apiso.xsd"&amp;gt;
   &amp;lt;gmd:fileIdentifier&amp;gt;
      &amp;lt;gco:CharacterString&amp;gt;https://hdl.handle.net/20.500.12085/1f97f2a1-75fc-4110-ae22-f873d7d86565@metadata&amp;lt;/gco:CharacterString&amp;gt;
   &amp;lt;/gmd:fileIdentifier&amp;gt;
   &amp;lt;gmd:language&amp;gt;
      &amp;lt;gmd:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeListValue="eng"&amp;gt;eng&amp;lt;/gmd:LanguageCode&amp;gt;
   &amp;lt;/gmd:language&amp;gt;
 &amp;lt;/gmd:MD_Metadata&amp;gt;&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;How can i replace the file under the xml tag. i tried using the replaceData() function but was unable to do it?&lt;/P&gt;</description>
      <pubDate>Thu, 06 Jan 2022 10:03:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131124#M63413</guid>
      <dc:creator>RehanChaudhary</dc:creator>
      <dc:date>2022-01-06T10:03:11Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace data in service definition draft file using python?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131637#M63440</link>
      <description>&lt;P&gt;Haven't tried this, but maybe you could treat this just as a text file and manipulate the tags using simple text reading, extracting/deleting the desired block, appending the replacement block, then appending the remaining xml? Or instead of replace, do a remove and insert?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 16:34:07 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131637#M63440</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-01-07T16:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace data in service definition draft file using python?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131641#M63441</link>
      <description>&lt;P&gt;@Anonymous User&amp;nbsp;but as soon as i parse the xml file to be replaced, it becomes an object. how can i use it to replce it?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 16:52:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131641#M63441</guid>
      <dc:creator>RehanChaudhary</dc:creator>
      <dc:date>2022-01-07T16:52:06Z</dc:date>
    </item>
    <item>
      <title>Re: How to replace data in service definition draft file using python?</title>
      <link>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131664#M63442</link>
      <description>&lt;P&gt;This lets me change the tags in the xml, but I'm not sure if it will validate:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;metadata_path=r'path to .sddraft'

tree = ET.parse(metadata_path)
root = tree.getroot()

xml_doc = """&amp;lt;gmd:MD_Metadata xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                 xmlns:gco="http://www.isotc211.org/2005/gco"
                 xmlns:gmd="http://www.isotc211.org/2005/gmd"
                 xmlns:srv="http://www.isotc211.org/2005/srv"
                 xmlns:gmx="http://www.isotc211.org/2005/gmx"
                 xmlns:gsr="http://www.isotc211.org/2005/gsr"
                 xmlns:gss="http://www.isotc211.org/2005/gss"
                 xmlns:gts="http://www.isotc211.org/2005/gts"
                 xmlns:gml="http://www.opengis.net/gml/3.2"
                 xmlns:xlink="http://www.w3.org/1999/xlink"
                 xmlns:xs="http://www.w3.org/2001/XMLSchema"
                 xsi:schemaLocation="http://www.isotc211.org/2005/gmd http://schemas.opengis.net/csw/2.0.2/profiles/apiso/1.0.0/apiso.xsd"&amp;gt; &amp;lt;gmd:fileIdentifier&amp;gt;
      &amp;lt;gco:CharacterString&amp;gt;https://hdl.handle.net/20.500.12085/1f97f2a1-75fc-4110-ae22-f873d7d86565@metadata&amp;lt;/gco:CharacterString&amp;gt;
   &amp;lt;/gmd:fileIdentifier&amp;gt;
   &amp;lt;gmd:language&amp;gt;
      &amp;lt;gmd:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeListValue="eng"&amp;gt;eng&amp;lt;/gmd:LanguageCode&amp;gt;
   &amp;lt;/gmd:language&amp;gt;
 &amp;lt;/gmd:MD_Metadata&amp;gt;"""

for xmldoc in root.iter('XmlDoc'):
    xmldoc.text = xml_doc

tree.write(r'output.sddraft')&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;DIV&gt;&lt;DIV&gt;&amp;nbsp;&lt;/DIV&gt;&lt;/DIV&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 17:59:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/how-to-replace-data-in-service-definition-draft/m-p/1131664#M63442</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-01-07T17:59:17Z</dc:date>
    </item>
  </channel>
</rss>

