Python Forum
How to read the XML files - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html)
+--- Thread: How to read the XML files (/thread-10716.html)



How to read the XML files - RajaPython - Jun-03-2018

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>


RE: How to read the XML files - buran - Jun-03-2018

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...


RE: How to read the XML files - RajaPython - Jun-03-2018

Hi Buran,

I am new to Python, i tired using different modules available in the python
but unable to read this xml file.


RE: How to read the XML files - buran - Jun-03-2018

(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


RE: How to read the XML files - snippsat - Jun-03-2018

(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'
>>> 



RE: How to read the XML files - RajaPython - Jun-03-2018

Thanks snippat. I will try this code from here. I was looking for a starting point


RE: How to read the XML files - RajaPython - Jun-04-2018

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>


RE: How to read the XML files - snippsat - Jun-05-2018

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]