Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xml - Find Element By tag using Python [duplicate]

I am trying extract some data from a bunch of xml files. Now, the issue is the structure of all the files is not exactly the same and thus, just iterating over the children and extracting the values is difficult.

Is there a getElementByTag() method for python for such xml documents? I have seen that such a method is available for C#, C++ users but couldn't find anything for Python.

Any help will be much appreciated!

like image 332
rishran Avatar asked Jul 11 '16 14:07

rishran


1 Answers

Yes, in the package xml.etree you can find the built-in function related to XML. (also available for python2)

The one specifically you are looking for is findall.

For example:

import xml.etree.ElementTree as ET
tree = ET.fromstring(some_xml_data)
all_name_elements = tree.findall('.//name')

With:

In [1]: some_xml_data = "<help><person><name>dean</name></person></help>"

I get the following:

In [10]: tree.findall(".//name")
Out[10]: [<Element 'name' at 0x7ff921edd390>]
like image 99
Dean Fenster Avatar answered Oct 08 '22 21:10

Dean Fenster