Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 parsing xml
#1
Hello
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"
print(g711p.attrib)

# in memory has been changed

#then, I write on the file and print to see the results
tree.write("SipServer1.config")

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

#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 -->
<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>
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
Quote
#2
# 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')

print(s.decode('utf-8'))
Quote

Top Page

Forum Jump:


Users browsing this thread: 1 Guest(s)