I am attempting to parse the below XML file but having difficulty getting a specific element value. I am trying to specify element 'Item_No_2' to get the related value <v>2222222222</v>
but am unable to do it using get.element('Item_No_2'). Am I using the get.element value incorrectly?
XML File:
<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="Data.xsl"?>
<abc>
<md>
<mi>
<datetime>20160822020003</datetime>
<period>3600</period>
<it>Item_No_1</it>
<it>Item_No_2</it>
<it>Item_No_3</it>
<it>Item_No_4</it>
<it>Item_No_5</it>
<it>Item_No_6</it>
<it>Item_No_7</it>
<ovalue>
<v>1111111111</v>
<v>2222222222</v>
<v>3333333333</v>
<v>4444444444</v>
<v>5555555555</v>
<v>6666666666</v>
<v>7777777777</v>
</ovalue>
</mi>
</md>
</abc>
My Code:
from xml.etree.ElementTree import parse
doc = parse('test.xml').getroot()
for element in doc.findall('md/mi/'):
print(element.text)
for element in doc.findall('md/mi/ovalue/'):
print(element.text)
The current output gets them separately but I can't seem to understand how to call a specific element value.
Output:
20160822020003
3600
Item_No_1
Item_No_2
Item_No_3
Item_No_4
Item_No_5
Item_No_6
Item_No_7
1111111111
2222222222
3333333333
4444444444
5555555555
6666666666
7777777777
Tried this but did not work:
for element in doc.findall('md/mi/ovalue/'):
print(element.get('Item_No_1'))