Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
xml replacement with python
#1
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
Reply
#2
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")
Reply
#3
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
Reply
#4
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"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  replacement for fpformat.extract GbSig1998 4 201 Apr-12-2024, 06:15 PM
Last Post: deanhystad
  String replacement in DB WJSwan 0 744 Dec-28-2022, 05:31 AM
Last Post: WJSwan
Exclamation IndexError: Replacement index 2 out of range for positional args tuple - help? MrKnd94 2 6,302 Oct-14-2022, 09:57 PM
Last Post: MrKnd94
  Extract the largest value from a group without replacement (beginner) preliator 1 2,073 Aug-12-2020, 01:56 PM
Last Post: DPaul
  Simple automated SoapAPI Call with single variable replacement from csv asaxty 1 2,094 Jun-30-2020, 06:38 PM
Last Post: asaxty
  line replacement help mdalireza 8 3,405 Nov-11-2019, 12:54 PM
Last Post: mdalireza
  Best replacement for pyzmail in lines 15 and 16 Pedroski55 0 2,486 Nov-03-2018, 06:12 AM
Last Post: Pedroski55
  Is pathlib a viable replacement for os.path? j.crater 4 9,959 Jan-13-2018, 09:49 AM
Last Post: Gribouillis
  Using python for text replacement omar 1 3,827 Dec-22-2016, 01:36 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020