I have an XML file (example below) - I am using Python 2.7
I want to change all occurences of stag to read prod. The word "stag" lies within a URL. I don't want to change the entire url just the occurence of the word stag - see example:
url=http://stag/comp/rest/services/editor/hyd/MapServer
want to change to:
url=http://prod/comp/rest/services/editor/hyd/MapServer
<Map FullExtent="-136163.9492,444360.64787994,-525467.175315,43020.05517682" InitialExtent="-32989.136772,6307.55418809,-4753.07696,5137.2783653">
<LayerList>
<Items>
<Item ID="17" />
<Item ID="20" IsExpanded="true" Name="Reference Data" Visible="true">
<Items>
<Item ID="30" />
<Item ID="34" VisibleInLayerList="false" />
<Item ID="22" VisibleInLayerList="false" />
<Item ID="41" VisibleInLayerList="false" />
<Item ID="16" />
<Item ID="37" />
<Item ID="24" />
<Item ID="39" />
<Item ID="32" />
<Item ID="28" />
<Item ID="26" />
</Items>
</Item>
<Item ID="19" IsExpanded="true" Name="Basemaps" Visible="true">
<Items>
<Item ID="12" />
<Item ID="11" />
</Items>
</Item>
</Items>
</LayerList>
<MapServices>
<MapService ConnectionString="url=http://stag/comp/rest/services/editor/hyd/MapServer" DefaultAllowSymbolization="true" DisplayName="hydrants" Function="Operational" ID="17" ImageFormat="Png32" ImpersonateWithActor="false" IncludeCopyright="false" IncludeInLayerList="true" InstantSearch="false" InstantSearchAttachments="false" IsExpanded="true" MaximumScale="500" MinimumScale="7500" Opacity="1" OverrideTemporalSettings="false" PasswordEncrypted="false" ProviderInvariantName="Geocortex.Gis.Services.ArcGisServer.Rest" SearchNonTextFields="true" SupportedImageHeight="0" SupportedImageWidth="0" UseHttpAuthentication="false" Visible="true">
<CachedServiceData />
<Layers>
<Layer AllowSymbolization="true" CanCopyFeature="true" DisplayName="Hydrants" FeatureDescription="<div><span style="font-family: Arial, Verdana; font-size: 13.3333px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><span style="font-weight: bold;">Hydrant ID: </span></span><font face="Arial, Verdana"><span style="font-size: 13.3333px;">{FIRE_HYDRANT_ID}</span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><span style="font-size: 13.3333px; font-family: Arial, Verdana;"><span style="font-weight: bold;"><br/></span></span></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><span style="font-size: 13.3333px; font-family: Arial, Verdana;"><span style="font-weight: bold;">Work Order Number</span>: {WORK_ORDER_NUM}</span></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><span style="font-size: 13.3333px; font-family: Arial, Verdana;"><br/></span></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><span style="font-weight: bold;">Make</span>: {MAKE}</span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><br/></span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><span style="font-weight: bold;">Elevation</span>: {ELEVATION}</span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><br/></span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><span style="font-weight: bold;">Hydrant Number</span>: {HYDRANT_NUM}</span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><br/></span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><span style="font-weight: bold;">Hydrant Size</span>: {HYDRANT_SIZE}</span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><br/></span></font></div><div style="font-family: Arial, Verdana; font-size: 10pt; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: normal; line-height: normal;"><font face="Arial, Verdana"><span style="font-size: 13.3333px;"><span style="font-weight: bold;">Install Year</span>: {INSTALL_YEAR}</span></font></div>" FeatureLabel="<b>Fire Hydrant</b>" Identifiable="true" IncludeInLayerList="true" IncludeInLegend="true" IsCatalogLayer="false" IsExpanded="true" LayerListID="30" Name="SJPWPUB.FIRE_HYDRANT" NativeID="0" Queryable="true" Searchable="true" ShowFeatureHyperlinks="ShowAll" ShowLabels="true" ShowMapTips="true" Snappable="true" SnappingEnabled="false" UnconfiguredFieldsCanSymbolizeClassBreaks="false" UnconfiguredFieldsCanSymbolizeUniqueValue="false" UnconfiguredFieldsSearchable="false" UnconfiguredFieldsVisible="false" UnconfiguredRelationshipsVisible="true" Visible="true">
<Fields>
<Field CanSymbolizeClassBreaks="false" CanSymbolizeUniqueValue="false" DisplayIndex="5" DisplayName="Hydrant Number" FocusField="false" Name="HYDRANT_NUM" Searchable="true" Visible="true" />
<Field CanSymbolizeClassBreaks="false" CanSymbolizeUniqueValue="false" DisplayIndex="2" DisplayName="Work Order Number" FocusField="false" Name="WORK_ORDER_NUM" Searchable="false" Visible="true" />
<Field CanSymbolizeClassBreaks="false" CanSymbolizeUniqueValue="false" DisplayIndex="3" DisplayName="Make" FocusField="false" Name="MAKE" Searchable="false" Visible="true" />
<Field CanSymbolizeClassBreaks="false" CanSymbolizeUniqueValue="false" DisplayIndex="6" DisplayName="Hydrant Size" FocusField="false" Name="HYDRANT_SIZE" Searchable="false" Visible="true" />
<Field CanSymbolizeClassBreaks="false" CanSymbolizeUniqueValue="false" DisplayIndex="7" DisplayName="Install Year" FocusField="false" Name="INSTALL_YEAR" Searchable="false" Visible="true" />
<Field CanSymbolizeClassBreaks="false" CanSymbolizeUniqueValue="false" DisplayIndex="4" DisplayName="Elevation" FocusField="false" Name="ELEVATION" Searchable="false" Visible="true" />
<Field CanSymbolizeClassBreaks="false" CanSymbolizeUniqueValue="false" DisplayIndex="1" DisplayName="Fire Hydrant ID" FocusField="false" Name="OBJECTID" Searchable="false" Visible="true" />
</Fields>
</Layer>
</Layers>
</MapService>
</Map>
I've tried a couple different methods but none seem to work. Here's my latest attempt:
Prod_xml = r"C:/Users/ba/Documents/temp/Prod/Projects/Site.xml"
import lxml.etree as ET
with open(Prod_xml, 'rb+') as f:
tree = ET.parse(f)
root = tree.getroot()
for elem in root.getiterator():
if elem.text:
elem.text.replace('stag', 'prod')
if elem.tail:
elem.tail.replace('stag', 'prod')
f.seek(0)
f.write(ET.tostring(tree, encoding='UTF-8', xml_declaration=True))
f.truncate()
This does not work. I am getting no error but the xml file is unchanged.
Any ideas on how to do this?
UPDATE
I'd like to use ElementTree option but I am getting the following error when I use the xml.etree.ElementTree script (It also removes the data within my xml file altogether):
Traceback (most recent call last):
File "F:/Applications/Geocortex/MovingGeocortexFiles.py", line 56, in <module>
ET.dump(f)
File "C:\Python27\ArcGIS10.3\lib\xml\etree\ElementTree.py", line 1164, in dump
elem.write(sys.stdout)
File "C:\Python27\ArcGIS10.3\lib\xml\etree\ElementTree.py", line 817, in write
self._root, encoding, default_namespace
File "C:\Python27\ArcGIS10.3\lib\xml\etree\ElementTree.py", line 876, in _namespaces
iterate = elem.getiterator # cET compatibility
AttributeError: 'file' object has no attribute 'getiterator'