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!
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>]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With