Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read the XML files
#5
(Jun-03-2018, 06:24 AM)RajaPython Wrote: I am new to Python, i tired using different modules available in the python
but unable to read this xml file.
Then you should post code and what you tried.
Both BeautifulSoup and lxml is fine for this.
from bs4 import BeautifulSoup

xml = '''\
<?xml version="1.0"?>
<OMeS version="2.3">
	<PMSetup startTime="2018-06-03T00:15:00.000-05:00:00" interval="15">
		<PMMOResult>
			<MO dimension="network_element">
				<DN>NTHLRFE-1/DDH-1/AFW-1</DN>
			</MO>
			<PMTarget measurementType="AFW">
				<AFW_TC_L_CANCEL>0</AFW_TC_L_CANCEL>
				<AFW_TC_L_REJECT>0</AFW_TC_L_REJECT>
				<AFW_TC_NOTICE>0</AFW_TC_NOTICE>
				<AFW_TC_R_REJECT>0</AFW_TC_R_REJECT>
				<AFW_TC_U_REJECT_IN>0</AFW_TC_U_REJECT_IN>
				<AFW_TC_U_REJECT_OUT>0</AFW_TC_U_REJECT_OUT>
				<SENT_TCABRT_POLICING>0</SENT_TCABRT_POLICING>
				<SNT_VIRGT_MULPLM>0</SNT_VIRGT_MULPLM>
			</PMTarget>
		</PMMOResult>
	</PMSetup>
	<PMSetup startTime="2018-06-03T00:15:00.000-05:00:00" interval="15">
		<PMMOResult>
			<MO dimension="network_element">
				<DN>NTHLRFE-1/BRM-hlrda001/CPU-12</DN>
			</MO>
			<PMTarget measurementType="CPU">
				<cpuLoad>1.93</cpuLoad>
				<cpuUsed>106</cpuUsed>
			</PMTarget>
		</PMMOResult>'''

soup = BeautifulSoup(xml, 'xml')
Test:
>>> pm = soup.find('PMTarget', {'measurementType': "AFW"})
>>> pm
<PMTarget measurementType="AFW">
<AFW_TC_L_CANCEL>0</AFW_TC_L_CANCEL>
<AFW_TC_L_REJECT>0</AFW_TC_L_REJECT>
<AFW_TC_NOTICE>0</AFW_TC_NOTICE>
<AFW_TC_R_REJECT>0</AFW_TC_R_REJECT>
<AFW_TC_U_REJECT_IN>0</AFW_TC_U_REJECT_IN>
<AFW_TC_U_REJECT_OUT>0</AFW_TC_U_REJECT_OUT>
<SENT_TCABRT_POLICING>0</SENT_TCABRT_POLICING>
<SNT_VIRGT_MULPLM>0</SNT_VIRGT_MULPLM>
</PMTarget>

>>> pm.find('AFW_TC_NOTICE')
<AFW_TC_NOTICE>0</AFW_TC_NOTICE>

>>> pm.find('AFW_TC_NOTICE').text
'0'
>>> 
Reply


Messages In This Thread
How to read the XML files - by RajaPython - Jun-03-2018, 06:19 AM
RE: How to read the XML files - by buran - Jun-03-2018, 06:20 AM
RE: How to read the XML files - by RajaPython - Jun-03-2018, 06:24 AM
RE: How to read the XML files - by buran - Jun-03-2018, 06:47 AM
RE: How to read the XML files - by snippsat - Jun-03-2018, 08:28 AM
RE: How to read the XML files - by RajaPython - Jun-03-2018, 05:03 PM
RE: How to read the XML files - by RajaPython - Jun-04-2018, 11:34 PM
RE: How to read the XML files - by snippsat - Jun-05-2018, 08:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask app cannot read js and css files. Aggam 0 1,651 Nov-04-2020, 12:33 PM
Last Post: Aggam

Forum Jump:

User Panel Messages

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