i have a xml file which looks like this :
<?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:MD_Metadata>
I want this piece of xml file to be added into the following xml file under the XmlDoc tag.
<?xml version="1.0"?>
<SVCManifest xmlns:typens="http://www.esri.com/schemas/ArcGIS/2.8.0"
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</CreaDate><CreaTime>18301700</CreaTime><ArcGISFormat>1.0</ArcGISFormat><SyncOnce>FALSE</SyncOnce><DataProperties><itemProps><itemName Sync="TRUE">schism.crf</itemName><itemLocation><linkage Sync="TRUE">file://C:\Users\Chaudhr1\arcgis\schism.crf</linkage><protocol Sync="TRUE">Local Area Network</protocol></itemLocation><imsContentType Sync="TRUE">002</imsContentType><nativeExtBox><westBL Sync="TRUE">9.778596</westBL><eastBL Sync="TRUE">10.237957</eastBL><southBL Sync="TRUE">53.410631</southBL><northBL Sync="TRUE">53.684061</northBL><exTypeCode Sync="TRUE">1</exTypeCode></nativeExtBox></itemProps><coordRef><type Sync="TRUE">Geographic</type><geogcsn Sync="TRUE">GCS_WGS_1984</geogcsn><csUnits Sync="TRUE">Angular Unit: Degree (0.017453)</csUnits><peXml Sync="TRUE">&lt;GeographicCoordinateSystem xsi:type='typens:GeographicCoordinateSystem' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xs='http://www.w3.org/2001/XMLSchema' xmlns:typens='http://www.esri.com/schemas/ArcGIS/2.8.0'&gt;&lt;WKT&gt;GEOGCS[&amp;quot;GCS_WGS_1984&amp;quot;,DATUM[&amp;quot;D_WGS_1984&amp;quot;,SPHEROID[&amp;quot;WGS_1984&amp;quot;,6378137.0,298.257223563]],PRIMEM[&amp;quot;Greenwich&amp;quot;,0.0],UNIT[&amp;quot;Degree&amp;quot;,0.0174532925199433],AUTHORITY[&amp;quot;EPSG&amp;quot;,4326]]&lt;/WKT&gt;&lt;XOrigin&gt;-400&lt;/XOrigin&gt;&lt;YOrigin&gt;-400&lt;/YOrigin&gt;&lt;XYScale&gt;11258999068426.238&lt;/XYScale&gt;&lt;ZOrigin&gt;-100000&lt;/ZOrigin&gt;&lt;ZScale&gt;10000&lt;/ZScale&gt;&lt;MOrigin&gt;-100000&lt;/MOrigin&gt;&lt;MScale&gt;10000&lt;/MScale&gt;&lt;XYTolerance&gt;8.983152841195215e-09&lt;/XYTolerance&gt;&lt;ZTolerance&gt;0.001&lt;/ZTolerance&gt;&lt;MTolerance&gt;0.001&lt;/MTolerance&gt;&lt;HighPrecision&gt;true&lt;/HighPrecision&gt;&lt;LeftLongitude&gt;-180&lt;/LeftLongitude&gt;&lt;WKID&gt;4326&lt;/WKID&gt;&lt;LatestWKID&gt;4326&lt;/LatestWKID&gt;&lt;/GeographicCoordinateSystem&gt;</peXml></coordRef><RasterProperties><General><PixelDepth Sync="TRUE">32</PixelDepth><HasColormap Sync="TRUE">FALSE</HasColormap><CompressionType Sync="TRUE">LERC2D</CompressionType><NumBands Sync="TRUE">1</NumBands><Format Sync="TRUE">Cache/LERC2D</Format><HasPyramids Sync="TRUE">TRUE</HasPyramids><SourceType Sync="TRUE">continuous</SourceType><PixelType Sync="TRUE">floating point</PixelType><NoDataValue Sync="TRUE">-3.4028231e+38</NoDataValue></General></RasterProperties><lineage><Process ToolSource="c:\program files\arcgis\pro\Resources\ArcToolbox\toolboxes\Data Management Tools.tbx\CopyRaster" Date="20211219" Time="183017">CopyRaster schism C:\Users\Chaudhr1\arcgis\schism.crf # # -3.402823E+38 NONE NONE # NONE NONE "Cloud raster format" NONE ALL_SLICES TRANSPOSE</Process></lineage></DataProperties><SyncDate>20211219</SyncDate><SyncTime>18301700</SyncTime><ModDate>20211219</ModDate><ModTime>18301700</ModTime></Esri><dataIdInfo><envirDesc Sync="TRUE">Microsoft Windows 10 Version 10.0 (Build 18363) ; Esri ArcGIS 12.8.0.29751</envirDesc><dataLang><languageCode value="eng" Sync="TRUE"></languageCode><countryCode value="USA" Sync="TRUE"></countryCode></dataLang><idCitation><resTitle Sync="TRUE">schism.crf</resTitle><presForm><PresFormCd value="005" Sync="TRUE"></PresFormCd></presForm></idCitation><spatRpType><SpatRepTypCd value="002" Sync="TRUE"></SpatRepTypCd></spatRpType><dataExt><geoEle><GeoBndBox esriExtentType="search"><exTypeCode Sync="TRUE">1</exTypeCode><westBL Sync="TRUE">9.778596</westBL><eastBL Sync="TRUE">10.237957</eastBL><northBL Sync="TRUE">53.684061</northBL><southBL Sync="TRUE">53.410631</southBL></GeoBndBox></geoEle></dataExt></dataIdInfo><mdLang><languageCode value="eng" Sync="TRUE"></languageCode><countryCode value="USA" Sync="TRUE"></countryCode></mdLang><mdChar><CharSetCd value="004" Sync="TRUE"></CharSetCd></mdChar><distInfo><distFormat><formatName Sync="TRUE">Raster Dataset</formatName></distFormat></distInfo><mdHrLv><ScopeCd value="005" Sync="TRUE"></ScopeCd></mdHrLv><mdHrLvName Sync="TRUE">dataset</mdHrLvName><refSysInfo><RefSystem><refSysID><identCode code="4326" Sync="TRUE"></identCode><idCodeSpace Sync="TRUE">EPSG</idCodeSpace><idVersion Sync="TRUE">6.2(3.0.1)</idVersion></refSysID></RefSystem></refSysInfo><spatRepInfo><Georect><cellGeo><CellGeoCd Sync="TRUE" value="002"></CellGeoCd></cellGeo><numDims Sync="TRUE">2</numDims><tranParaAv Sync="TRUE">1</tranParaAv><chkPtAv Sync="TRUE">0</chkPtAv><cornerPts><pos Sync="TRUE">9.778596 53.410631</pos></cornerPts><cornerPts><pos Sync="TRUE">9.778596 53.684061</pos></cornerPts><cornerPts><pos Sync="TRUE">10.237957 53.684061</pos></cornerPts><cornerPts><pos Sync="TRUE">10.237957 53.410631</pos></cornerPts><centerPt><pos Sync="TRUE">10.008276 53.547346</pos></centerPt><axisDimension type="002"><dimSize Sync="TRUE">504</dimSize><dimResol><value Sync="TRUE" uom="deg">0.000911</value></dimResol></axisDimension><axisDimension type="001"><dimSize Sync="TRUE">300</dimSize><dimResol><value Sync="TRUE" uom="deg">0.000911</value></dimResol></axisDimension><ptInPixel><PixOrientCd Sync="TRUE" value="001"></PixOrientCd></ptInPixel></Georect></spatRepInfo><contInfo><ImgDesc><contentTyp><ContentTypCd Sync="TRUE" value="001"></ContentTypCd></contentTyp><covDim><Band><dimDescrp Sync="TRUE">Band_1</dimDescrp><maxVal Sync="TRUE">6.000000</maxVal><minVal Sync="TRUE">2.000000</minVal><bitsPerVal Sync="TRUE">32</bitsPerVal><valUnit><UOM type="length"></UOM></valUnit></Band></covDim></ImgDesc></contInfo><mdDateSt Sync="TRUE">20211219</mdDateSt></metadata>
</XmlDoc>
</Metadata>
</SVCManifest>
i tried doing this with the following python code
import xml.dom.minidom as DOM
import shutil
import xml.etree.ElementTree as ET
metadata_path=r"C:\Users\arcgis\UnidataDD2MI_result.xml"
new_metadata=DOM.parse(metadata_path)
output_draft = r"C:\Users\arcgis\airquality.sddraft"
doc = DOM.parse(output_draft)
meta=doc.getElementsByTagName('XmlDoc')
for metadata in meta:
if metadata.parentNode.tagName == 'SVCManifest':
if metadata.hasChildNodes():
metadata.firstChild.data = new_metadata
The above code did not change anything in the xml file. How can i replace the contents of the xml file?
The final file should look something like this :
<?xml version="1.0"?>
<SVCManifest xmlns:typens="http://www.esri.com/schemas/ArcGIS/2.8.0"
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" 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:MD_Metadata>
</XmlDoc>
</Metadata>
</SVCManifest>
source https://stackoverflow.com/questions/70414283/how-can-i-replace-the-contents-of-one-xml-file-with-another-in-python
Comments
Post a Comment