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 :
<SVCManifest xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="typens:SVCManifest">
<Databases xsi:type="typens:ArrayOfSVCDatabase" />
<Resources xsi:type="typens:ArrayOfSVCResource">
<SVCResource xsi:type="typens:SVCResource">
<ID>{429221BF-D0A1-40D8-9DC1-B41D269E95C7}</ID>
<Name>test.crf</Name>
<Metadata xsi:type="typens:XmlPropertySet">
<XmlDoc><?xml version="1.0"?>
<metadata xml:lang="en"><Esri><CreaDate>20211219</metadata>
</XmlDoc>
</Metadata>
</SVCManifest>
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.
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)
This is the file i want to have under the tag:
<?xml version="1.0" encoding="UTF-8"?>
<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">
<gmd:fileIdentifier>
<gco:CharacterString>https://hdl.handle.net/20.500.12085/1f97f2a1-75fc-4110-ae22-f873d7d86565@metadata</gco:CharacterString>
</gmd:fileIdentifier>
<gmd:language>
<gmd:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeListValue="eng">eng</gmd:LanguageCode>
</gmd:language>
</gmd:MD_Metadata>
How can i replace the file under the xml tag. i tried using the replaceData() function but was unable to do it?
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?
@Anonymous User but as soon as i parse the xml file to be replaced, it becomes an object. how can i use it to replce it?
This lets me change the tags in the xml, but I'm not sure if it will validate:
metadata_path=r'path to .sddraft'
tree = ET.parse(metadata_path)
root = tree.getroot()
xml_doc = """<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"> <gmd:fileIdentifier>
<gco:CharacterString>https://hdl.handle.net/20.500.12085/1f97f2a1-75fc-4110-ae22-f873d7d86565@metadata</gco:CharacterString>
</gmd:fileIdentifier>
<gmd:language>
<gmd:LanguageCode codeList="http://www.loc.gov/standards/iso639-2/" codeListValue="eng">eng</gmd:LanguageCode>
</gmd:language>
</gmd:MD_Metadata>"""
for xmldoc in root.iter('XmlDoc'):
xmldoc.text = xml_doc
tree.write(r'output.sddraft')