Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 parsing xml
I have try to create a code to parse a xml file and find a key word PCMU which has the value True, then I want to change the value for False
At the beginning, I tried to use a library minidom but I found this other option (import xml.etree.ElementTree as ET) which is maybe easy to handled (I am new with python)

this is my code:

import xml.etree.ElementTree as ET 
import os

#here I go to the path where my file is located
os.chdir(r'C:/ProgramData/Genetec Sipelia/SipServer')

#read the file
tree = ET.parse('SipServer1.config')
root = tree.getroot()

# I created ethe variable g711p and I assigned the value Name which has the value PCMU and it is located in this level
#I am looking for this line <Codec Name="PCMU" Enabled="True"/>
g711p= root.find("./Configuration/Codecs/AudioCodecs/Codec[@Name='PCMU']")

#I set the attributs, originally is TRue but I will change by False and I print it
g711p.attrib["Name"] = "PCMU"
g711p.attrib["Enabled"] = "False"

# in memory has been changed

#then, I write on the file and print to see the results

for codec in root.iter('Codec'):

#here is my issue, the position has been changed 
# in my new file appears as <Codec Enabled="False" Name="PCMU" /> the value Enabled appears before Name

How can read without change the position?
I will appreciate any help
Please, follow the xml below

<?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 -->
<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 -->
<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"/>
<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"/>
<!-- The default method name is 'None' -->
<NatTraversalMethod Value="None"/>
Larz60+ wrote Mar-05-2019, 01:29 AM:
It's not wrong (really) to post the XML directly in the post, but there's less clutter, and much less chance for error if you attach the file instead.
This can be found if you click new reply rather than using quick reply
# xml_as_string = """ Your xml goes here"""
# root = ET.fromstring(xml_as_string)

root = et.fromstring(data)
for el in root.findall(".//Codec[@Name='PCMU']"):
    if el.attrib['Enabled'].lower().strip() == "true":
        el.attrib['Enabled'] = "False"
    print('Found element: {}'.format(el.attrib))

s = ET.tostring(root, encoding='utf8', method='xml')


Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  string parsing question ( I think) diggee17 4 150 Jul-24-2019, 02:37 PM
Last Post: diggee17
  Parsing Soap XML response grootkarzijn 7 421 Jun-14-2019, 01:30 AM
Last Post: snippsat
  Parsing an MBOX file Oliver 1 418 May-26-2019, 07:12 AM
Last Post: heiner55
  XML Parsing Child karthi_python 1 167 May-16-2019, 01:37 PM
Last Post: karthi_python
  Issues parsing the response from a request garnold 3 266 May-14-2019, 12:39 PM
Last Post: snippsat
  parsing logical expression with pyparsing palo173 2 300 May-13-2019, 09:22 AM
Last Post: palo173
  Parsing Attached .MSG Files with Python3 ericl42 1 278 Apr-12-2019, 06:28 PM
Last Post: ericl42
  parsing complex text file anna 1 260 Apr-10-2019, 09:54 PM
Last Post: Larz60+
  Parsing Large Numbers Question amyvaulhausen 5 454 Mar-24-2019, 08:46 AM
Last Post: samsonite
  parsing xml and outpur as csv kentck86 1 256 Mar-18-2019, 02:10 PM
Last Post: snippsat

Forum Jump:

Users browsing this thread: 1 Guest(s)