How do I get specific nodes in an XML file in Python?

Python Methods and Functions

Using the xml library, you can get any node you want from the xml file. But in order to retrieve a given node, you need to know how to use the xpath to get it. You can read more about XPath here: https://www.w3schools.com/xml/xml_xpath.asp.


Example

For example, suppose you have an XML file with the following structure,



 & lt; bookstore & gt; & lt; book category = "cooking" & gt; & lt; title lang = "en" & gt; Everyday Italian & lt; / title & gt; & lt; author & gt; Giada De Laurentiis & lt; / author & gt; & lt; year & gt; 2005 & lt; / year & gt; & lt; price & gt; 30.00 & lt; / price & gt; & lt; / book & gt; & lt; book category = "children" & gt; & lt; title lang = "en" & gt; Harry Potter & lt; / title & gt; & lt; author & gt; J K. Rowling & lt; / author & gt; & lt; year & gt; 2005 & lt; / year & gt; & lt; price & gt; 29.99 & lt; / price & gt; & lt; / book & gt; & lt; / bookstore & gt; 

And you want to extract all header nodes with lang en attribute, then you have the code &



 from xml.etree. ElementTree import ElementTree tree = ElementTree () root = tree.parse ("my_file.xml") for node in root.findall ("// title [@ lang =' en'] "): for type in node.getchildren () : print (type.text) 







Get Solution for free from DataCamp guru