Home > database >  How to get value of element in XML file using Python
How to get value of element in XML file using Python

Time:08-26

Below is the XML file I am trying to read.

<annotation>
    <folder>JPEGImages</folder>
    <filename>L00030.png</filename>
    <path>D:\Annotation\Station1\S12508\JPEGImages\L00030.png</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1280</width>
        <height>720</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>bezel</name>
        <pose>Unspecified</pose>
    </object>
    <object>
        <name>plate</name>
        <pose>Unspecified</pose>
    </object>
</annotation>

In the above XML, I am trying to read the value of object with name, so in this case, it will be bezel and plate. I am trying to do this using xml Python package

xmlTree = ET.parse("file.xml")

elemList = []
for elem in xmlTree.iter():
    elemList.append(elem.tag)

This way I am able to get all the elements but unable to get the value. What is the best way to achieve this?

CodePudding user response:

see below

import xml.etree.ElementTree as ET

xml = '''<annotation>
    <folder>JPEGImages</folder>
    <filename>L00030.png</filename>
    <path>D:\Annotation\Station1\S12508\JPEGImages\L00030.png</path>
    <source>
        <database>Unknown</database>
    </source>
    <size>
        <width>1280</width>
        <height>720</height>
        <depth>3</depth>
    </size>
    <segmented>0</segmented>
    <object>
        <name>bezel</name>
        <pose>Unspecified</pose>
    </object>
    <object>
        <name>plate</name>
        <pose>Unspecified</pose>
    </object>
</annotation>
'''

root = ET.fromstring(xml)
values = [s.find('name').text for s in root.findall('.//object') if s.find('name') is not None]
print(values)

output

['bezel', 'plate']

CodePudding user response:

change this line:

elemList.append(elem.tag)

to this:

elemList.append(elem.text)

or this, if you want both:

elemList.append([elem.tag, elem.text])

elem.tag is the name of the tag, so in <foo>bar</foo> it would be foo.

elem.text is whatever was inside the tag, so in <foo>bar</foo> it would be bar.

Have a look at this

  • Related