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 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 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 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 Count occurrences of an element in a list in Python A common task when working with lists is to count how many times a specific element appears. In Python, we have several ways to count occurrences of an element using both built-in and custom methods.The simplest and most straightforward way to count occurrences of an element in a list is by using th 2 min read How to store XML data into a MySQL database using Python? In this article, we are going to store XML data into the MySQL database using python through XAMPP server. So we are taking student XML data and storing the values into the database. RequirementsXAMPP server: It is a cross-platform web server used to develop and test programs on a local server. It i 3 min read Python - Counter.items(), Counter.keys() and Counter.values() Counter class is a special type of object data-set provided with the collections module in Python3. Collections module provides the user with specialized container datatypes, thus, providing an alternative to Pythonâs general-purpose built-ins like dictionaries, lists and tuples. Counter is a sub-cl 3 min read Python | Count occurrences of an element in a Tuple In this program, we need to accept a tuple and then find the number of times an item is present in the tuple. This can be done in various ways, but in this article, we will see how this can be done using a simple approach and how inbuilt functions can be used to solve this problem. Examples: Tuple: 3 min read How to create Pandas DataFrame from nested XML? In this article, we will learn how to create Pandas DataFrame from nested XML. We will use the xml.etree.ElementTree module, which is a built-in module in Python for parsing or reading information from the XML file. The ElementTree represents the XML document as a tree and the Element represents onl 3 min read Like