How to parse XML and count instances of a particular node attribute in Python? Last Updated : 13 Dec, 2022 Comments Improve Suggest changes Like Article Like Report In this article, we will see how to parse XML and count instances of a particular node attribute in Python. What is XML? Extensible Markup Language (XML) Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is a markup language like HTML, and It is designed to store and transport data. Here, we will use built-in XML modules in python for parsing XML and then counting the instances of a node. We use ElementTree XML API and minidom API to parse our XML file. XML code for a note is given below: It should be saved as a country_data.xml file in the same directory. XML <?xml version="1.0"?> <data> <country name="France"> <rank>1</rank> <year>2008</year> <gdppc>141100</gdppc> <neighbor name="Germany" direction="E"/> <neighbor name="Spain" direction="N"/> </country> <country name="Poland"> <rank>4</rank> <year>2011</year> <gdppc>59900</gdppc> <neighbor name="Germany" direction="W"/> </country> <country name="Italy"> <rank>68</rank> <year>2015</year> <gdppc>13600</gdppc> <neighbor name="France" direction="N"/> </country> </data> Example 1: In this example, We will use xml.etree.ElementTree module for parsing our XML file and storing in tree variable and after that we will find all the instances of a particular node attribute with the python findall() function of this module. Now we iterate over a list and check for a particular node attribute value if it matches then we will increment count as 1 to our variable. Python3 # Importing our module import xml.etree.ElementTree as ET # Finding the Node Attribute with name tag # neighbor and name value as "Germany" Name_attribute = "France"; # Parsing our xml file tree = ET.parse('country_data.xml') root = tree.getroot(); # Counting the instance of Node attribute with findall NO_node = 0 ; for instance in root.findall('country/neighbor'): # Checking for the particular Node Attribute if instance.get('name') == Name_attribute: NO_node+=1; # Printing Number of nodes print ("total instance of given node attribute is : ", NO_node) Output: total instance of given node attribute is : 1Example 2: In this example, we will parse our XML file with the help of minidom module and assign this to the doc variable, getElementsByTagName() function returns a list of instances of a particular node. Now we iterate over a list and check for a particular node attribute value if it matches then we will increment count as 1 to our variable. Python3 # Importing our module from xml.dom import minidom # Finding the node instance with name "Germany" Name_attribute = "Germany"; # Parsing our xml file doc = minidom.parse('country_data.xml') root = doc.getElementsByTagName('neighbor') Number_attributes = 0; for i in root: # print ctypes.cast(i, ctypes.py_object).value if i.attributes['name'].value == Name_attribute: Number_attributes += 1; # Printing Number of nodes print ("Total instance of Particular node attribute is : " ,Number_attributes) Output: Total instance of Particular node attribute is : 2 Comment More infoAdvertise with us Next Article How to parse XML and count instances of a particular node attribute in Python? S satyam00so Follow Improve Article Tags : Python Python-XML Practice Tags : python Similar Reads How to Get a List of Class Attributes in Python? A class is a user-defined blueprint or prototype from which objects are created. Classes provide a means of bundling data and functionality together. Creating a new class creates a new type of object, allowing new instances of that type to be made. Each class instance can have attributes attached to 4 min read How to Parse and Modify XML in Python? XML stands for Extensible Markup Language. It was designed to store and transport data. It was designed to be both human- and machine-readable. Thatâs why, the design goals of XML emphasize simplicity, generality, and usability across the Internet. Note: For more information, refer to XML | Basics H 4 min read Find the Frequency of a Particular Word in a Cell in an Excel Table in Python In this article, we'll look at how to use Python to find the number of times a word appears in a cell of an Excel file. Before we begin with the steps of the solution, the following modules/libraries must be installed. We will use the following sample Excel file to determine the frequency of the inp 5 min read How to Count the Number of Rows in a MySQL Table in Python? MySQL server is an open-source relational database management system which is a major support for web-based applications. Databases and related tables are the main component of many websites and applications as the data is stored and exchanged over the web. In order to access MySQL databases from a 2 min read How to count the number of lines in a CSV file in Python? Counting the number of lines in a CSV file in Python means determining how many rows the file contains, including or excluding the header depending on your needs. For example, if your CSV file has 100 data rows plus one header row, the total line count is 101. We will use the following dataset to de 2 min read How to convert lists to XML in Python? In this article, the task is to convert a given list to XML in Python. But first, let's discuss what is an XML? It could also be terminology that defines a gaggle of rules for encoding documents during a format that's both human-readable and machine-readable. The design goals of XML specialize in si 3 min read Count occurrences of a character in string in Python We are given a string, and our task is to count how many times a specific character appears in it using Python. This can be done using methods like .count(), loops, or collections.Counter. For example, in the string "banana", using "banana".count('a') will return 3 since the letter 'a' appears three 2 min read Get index in the list of objects by attribute in Python In this article, we'll look at how to find the index of an item in a list using an attribute in Python. We'll use the enumerate function to do this. The enumerate() function produces a counter that counts how many times a loop has been iterated. We don't need to import additional libraries to utili 2 min read Count the number of times a letter appears in a text file in Python In this article, we will be learning different approaches to count the number of times a letter appears in a text file in Python. Below is the content of the text file gfg.txt that we are going to use in the below programs: Now we will discuss various approaches to get the frequency of a letter in a 3 min read Parsing and converting HTML documents to XML format using Python In this article, we are going to see how to parse and convert HTML documents to XML format using Python. It can be done in these ways: Using Ixml module.Using Beautifulsoup module.Method 1: Using the Python lxml library In this approach, we will use Python's lxml library to parse the HTML document a 3 min read Like