Open In App

How to parse XML and count instances of a particular node attribute in Python?

Last Updated : 13 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
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 :  1

Example 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


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 Here we consider that the XML file is present in th
4 min read
Parse XML using Minidom in Python
DOM (document object model) is a cross-language API from W3C i.e. World Wide Web Consortium for accessing and modifying XML documents. Python enables you to parse XML files with the help of xml.dom.minidom, which is the minimal implementation of the DOM interface. It is simpler than the full DOM API and should be considered as smaller. Steps for Pa
1 min read
How to count number of instances of a class in Python?
Instances of a class mean the objects created for a particular class. A single class can have multiple objects of it. Here, we will find the count of the number of instances of a class in Python. Approach: Whenever an object is created, the constructor of that particular class is called.Constructor is a function whose name is the same as that of cl
1 min read
Python | Avoiding class data shared among the instances
Class attributes belong to the class itself and they will be shared by all the instances and hence contains same value of each instance. Such attributes are defined in the class body parts usually at the top, for legibility. Suppose we have the following code snippet : C/C++ Code # Python code to demonstrate # the working of the sharing # of data v
2 min read
PYGLET – Getting font instances used in Formatted Document
In this article we will see how we can get all the font used in the formatted document in PYGLET module in python. Pyglet is easy to use but powerful library for developing visually rich GUI applications like games, multimedia etc. A window is a "heavyweight" object occupying operating system resources. Windows may appear as floating regions or can
3 min read
Python | Count of elements matching particular condition
Checking a number/element by a condition is a common problem one faces and is done in almost every program. Sometimes we also require to get the totals that match the particular condition to have a distinguish which to not match for further utilization. Lets discuss certain ways in which this task can be achieved. Method #1 : Using sum() + generato
5 min read
Python | Count keys with particular value in dictionary
Sometimes, while working with Python dictionaries, we can come across a problem in which we have a particular value, and we need to find frequency if it's occurrence. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop This problem can be solved using naive method of loop. In this we just iterate through each key i
5 min read
Python - Sort by a particular digit count in elements
Given a list of elements, sort by K digit in each element. Examples: Input : test_list = [4322, 2122, 123, 1344], K = 2 Output : [1344, 123, 4322, 2122] Explanation : 0 &lt; 1 &lt; 2 &lt; 3, sorted by count of 2 in each element. Input : test_list = [4322, 2122, 1344], K = 2 Output : [1344, 4322, 2122] Explanation : 0 &lt; 2 &lt; 3, sorted by count
5 min read
Python Program to Count date on a particular weekday in given range of Years
Given a date, our task is to get the weekday index and year range, and compute the total occurrence of the date on a particular weekday in the year range. Example: Input : date = 13, weekdy = 5, strt, end = 1950, 2020Output : 120Explanation : Total dates with Friday 13 from 1950 - 2020 is 120. Input : date = 13, weekdy = 1, strt, end = 1950, 2050Ou
5 min read
Python | Parse a website with regex and urllib
Let's discuss the concept of parsing using python. In python we have lot of modules but for parsing we only need urllib and re i.e regular expression. By using both of these libraries we can fetch the data on web pages. Note that parsing of websites means that fetch the whole source code and that we want to search using a given url link, it will gi
2 min read
Article Tags :
Practice Tags :