Using xpath to get value of a nested element name using tag named - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Using xpath to get value of a nested element name using tag named (/thread-15453.html) |
Using xpath to get value of a nested element name using tag named - xq1xq1xq1 - Jan-17-2019 I have a nested XML tag file that parses correctly with elementtree. I have been unable to find a method of getting the text value for an element nested in the file. If I do a for loop I can do a findall and retrieve my values but would like to use a static mapping to get the data as it will be much more efficient. Can anyone please tell me how to do this? RE: Using xpath to get value of a nested element name using tag named - Larz60+ - Jan-17-2019 Please show code RE: Using xpath to get value of a nested element name using tag named - xq1xq1xq1 - Jan-18-2019 This is an example XML file that I wish to parse. I would like to be able to pick out the item data/country/year and get the values: 2008 2011 2011 I would also like to be able to pick out the item data/country/year for country = "Panama" and get 2011 I think I can use something like: year = country.find('year').textto get the year for each country but have not been successful in figuring out the syntax to retrieve this for all countries or even just for Panama which is my objective. <?xml version="1.0"?> <data> <country name="Liechtenstein"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Austria" direction="E"/> <neighbor name="Switzerland" direction="W"/> </country> <country name="Singapore"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Malaysia" direction="N"/> </country> <country name="Panama"> <rank>68</rank> <year>2011</year> <gdppc>13600</gdppc> <neighbor name="Costa Rica" direction="W"/> <neighbor name="Colombia" direction="E"/> </country> </data>This is the start of the code that I am writing: #!/usr/local/bin/python import xml.etree.ElementTree as ET tree = ET.parse('/root/test/test_data_fixed.xml') root = tree.getroot() for country in root.findall('country'): rank = country.find('rank').text name = country.get('name') print(name, rank) print root.findall("./country/neighbor") print root.findall("./country/year.value")Result is: ('Liechtenstein', '1') ('Singapore', '4') ('Panama', '68') [<Element neighbor at 2315fc8>, <Element neighbor at 231d050>, <Element neighbor at 231d1b8>, <Element neighbor at 231d320>, <Element neighbor at 231d368>] []Thanks in advance RE: Using xpath to get value of a nested element name using tag named - Larz60+ - Jan-18-2019 **Note**: I named my file 'stuff.xml' change this line to your filename You may have to install lxml if you don't already have it: pip install lxmland finally, I use f-string which requires python 3.6 or newer if you have to use earlier version for some reason, change line 15 to: print('tag: {}, value: {}'.format(element.tag, text))You should use lxml: from lxml import etree import os def parse_xml(filename): os.chdir(os.path.abspath(os.path.dirname(__file__))) with open(filename) as fp: xml_text = fp.read() tree = etree.fromstring(xml_text) for kid in tree.getchildren(): for element in kid.getchildren(): text = None if element.text: text = element.text.strip() print(f'tag: {element.tag}, value: {text}') if __name__ == "__main__": parse_xml("stuff.xml")output:
|