Python Forum

Full Version: xml replacement with python
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello
I try to use python to replace part of the xml file without override the rest of the file,
the code doesn't show any error but it is not replacing the code
My idea is find the tag <AudioCodeco> and then, I will change the first line which is:
<Codec Name="PCMA" Enabled="True"/>
by
<Codec Name="PCMA" Enabled="False"/>

As you could notice, I want to change True by False

If you have a better idea, I will appreciate your orientation.

This is my code

# import element tree
import xml.etree.ElementTree as ET 


#import xml file
tree = ET.parse(r'C:\ProgramData\Genetec Sipelia\SipServer\SipServer.config')
root = tree.getroot()
#replace bounding box with new coordinates

elem = tree.findall('AudioCodecs')

for elem1 in elem:
    elem1.txt = 'Codec Name="PCMA" Enabled="False"'
 
and this is my xml

<?xml version="1.0"?>
<!-- WARNING: Those configuration represent the the default values for Sipelia SIP Server -->
<!-- WARNING: It is very recommended to make a backup of this file before doing any changes -->
<SipeliaSipServer>
<Configuration Version="2.0.7">
<!-- Values must be between 0 and 65535 -->
<MinimumPortRange Value="20000"/>
<MaximumPortRange Value="20500"/>
<!-- Value is in seconds. Minimum value is 40 -->
<TrunkStateTimeout Value="60"/>
<!-- Value is in seconds. Minimum value is 0 -->
<DeviceRegistrationMargin Value="60"/>
<!-- Value is enabled or disabled. Used to resolve invalid SIP contact header fields. -->
<InvalidContactFieldResolution Enabled="False" />
<!-- Sipelia SIP Server Audio and Video codecs -->
<!-- WARNING: Edit only the 'Enabled' attribute value -->
<Codecs>
<VideoCodecs>
<Codec Name="H263-1998" Enabled="True"/>
<Codec Name="H264" Enabled="True" KeyframeGenerationInterval="4"/> <!-- KeyframeGenerationInterval: When transcoding to H264 this value will be used to generate the keyframes. The default value for K-frame is 4s. -->
<Codec Name="H263" Enabled="True"/>
</VideoCodecs>
<AudioCodecs>
<Codec Name="PCMA" Enabled="True"/>
<Codec Name="PCMU" Enabled="True"/>
<Codec Name="telephone-event" Enabled="True"/>
<Codec Name="iLBC" Enabled="True"/>
<Codec Name="G722" Enabled="True"/>
<Codec Name="GSM" Enabled="True"/>
<Codec Name="SPEEX_Narrowband" Enabled="False"/>
<Codec Name="SPEEX_Wideband" Enabled="True"/>
<Codec Name="SPEEX_Ultrawideband" Enabled="True"/>
<Codec Name="L16" Enabled="False"/>
<Codec Name="L16_44_1" Enabled="True"/>
<Codec Name="G728" Enabled="False"/>
<Codec Name="G723" Enabled="False"/>
<Codec Name="G726-16" Enabled="True"/>
<Codec Name="G726-24" Enabled="False"/>
<Codec Name="G726-32" Enabled="False"/>
<Codec Name="G726-40" Enabled="False"/>
<Codec Name="G729" Enabled="False"/>
</AudioCodecs>
</Codecs>
<!-- The default method name is 'None' -->
<NatTraversalMethod Value="None"/>
</Configuration>
</SipeliaSipServer>



Regards,

Jose
Element.text doesn't correspond to what you want to change. "Enabled" is an attribute which is stored as a dictionary as Element.attrib.

Try this:

# import element tree
import xml.etree.ElementTree as ET 
 
 
#import xml file
tree = ET.parse(r'C:\ProgramData\Genetec Sipelia\SipServer\SipServer.config')
root = tree.getroot()
#replace bounding box with new coordinates
 
elem = tree.findall('AudioCodecs')
 
for elem1 in elem:
    elem1.attrib.set("Enabled", "False")
Hello, thanks for the info but it didn't work either.
I just want to clarify what I want

I have a line in a xml file:
<Codec Name="PCMA" Enabled="True"/>

I want to change with this:
<Codec Name="PCMA" Enabled="False"/>

I want to change the word True by False
I will appreciate any help
I reviewed in more detail, the XPath for tree.findall() was returning an empty list. This works on my side:

# import element tree
import xml.etree.ElementTree as ET 
  
  
#import xml file
tree = ET.parse(r'C:\ProgramData\Genetec Sipelia\SipServer\SipServer.config')
root = tree.getroot()
#replace bounding box with new coordinates
  
elem = tree.find('.//AudioCodecs')
  
for elem1 in elem:
    if elem1.attrib["Name"] == "PCMA":
        elem1.attrib["Enabled"] = "False"