Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to read the XML files
#1
Information 
Need help in reading the xml file and converting into dictionaries

XmL file:
"
<?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>
Reply
#2
Great. What have you tried? Post your code in python tags. If you get exceptions, post the full traceback in error tags. Ask specific questions...
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
Hi Buran,

I am new to Python, i tired using different modules available in the python
but unable to read this xml file.
Reply
#4
(Jun-03-2018, 06:24 AM)RajaPython Wrote: i tired using different modules available in the python
(Jun-03-2018, 06:20 AM)buran Wrote: What have you tried? Post your code in python tags. If you get exceptions, post the full traceback in error tags. Ask specific questions...

We are glad to help, but we are not going to do it for you...

https://www.google.com/search?q=xml+to+dict+python
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#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
#6
Thanks snippat. I will try this code from here. I was looking for a starting point
Reply
#7
Hi Snippat,

In my XML file i have below data, multiple object having similar data with different object IDs

from bs4 import BeautifulSoup as bsp

xml_file=open("E:\\Python\\MyLearning\\TestFiles\\MY.xml", "r+")
soup_xml = bsp(xml_file, 'xml')

pm=soup_xml.find_all('PMTarget', {'MType':"ABC"})
data=pm.find('ID')
print (data)
Error:
Traceback (most recent call last): File "E:/Python/MyLearning/Learning.py", line 230, in <module> data=pm.find('ID') File "C:\Python3.6.5\lib\site-packages\bs4\element.py", line 1807, in __getattr__ "ResultSet object has no attribute '%s'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?" % key AttributeError: ResultSet object has no attribute 'find'. You're probably treating a list of items like a single item. Did you call find_all() when you meant to call find()?
XML FILE:
<DN>test-1/check-1</DN>
</MO>
<PMTarget MType="ABC">
<BYTES_IN>16365</BYTES_IN>
<BYTES_OUT>1560</BYTES_OUT>
<CONNECTIONS>8</CONNECTIONS>
<CONN_OPENED>0</CONN_OPENED>
<EXECUTION_T>525</EXECUTION_T>
<ID>8</ID>
</PMTarget>
</PMMOResult>
<PMMOResult>
<MO dimension="network_element">
<DN>test-1/check-2</DN>
</MO>
<PMTarget MType="ABC">
<BYTES_IN>19935</BYTES_IN>
<BYTES_OUT>1920</BYTES_OUT>
<CONNECTIONS>12</CONNECTIONS>
<CONN_OPENED>0</CONN_OPENED>
<ERRORS>0</ERRORS>
<EXECUTION_T>435</EXECUTION_T>
<ID>7</ID>
</PMTarget>
Reply
#8
find_all return a list.
So can not use find on that list.
Have to iterate over list,or take out elements from list.
>>> pm = soup.find_all('pmtarget', {'mtype':"ABC"})
>>> pm[0].find('id')
<id>8</id>
>>> pm[1].find('id')
<id>7</id>
>>> pm[1].find('id').text
'7'

>>> [i.find('id').text for i in pm]
['8', '7']
>>> [int(i.find('id').text) for i in pm]
[8, 7]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask app cannot read js and css files. Aggam 0 1,600 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