Reputation: 13
I have problem with extracting data from a xml file what contains the lxml. I've tried to extract data with use of lxml library but i have no output at all.
from lxml import etree
tree = etree.parse("ifm-00028A-20170711-IODD1.1.xml")
root = tree.getroot()
levels = root.findall('DeviceIdentity', root.nsmap)
for DeviceIdentity in levels:
data_1 = int(DeviceIdentity.find('deviceId').text)
print(data_1)
[IODD][1]
I need for example get the value from the vendorId and deviceId
Thanks for help !
Sample of the xml file https://i.sstatic.net/Ih4Tk.png
Upvotes: 1
Views: 1001
Reputation:
There are two bugs here.
First, findall
findall only searches the immediate descendants of an element if it is given a tag name. You can use XPath expressions to search deeper. So your findall
should be
levels = root.findall('.//DeviceIdentity', root.nsmap)
Then, deviceId
is an attribute. You should be able to find it in the element's attrib dictionary. Assuming the rest of your code is correct, it would look something like this:
for DeviceIdentity in levels:
data_1 = int(DeviceIdentity.attrib['deviceId'])
print(data_1)
(In future questions, it would have been helpful to include the sample XML as text, and to be more specific than "no output at all")
Upvotes: 1