# XML 2 Find the Maximum Depth in Python HackerRank Solution Hello Programmers, In this post, you will know how to solve the XML 2 Find the Maximum Depth in Python HackerRank Solution. This problem is a part of the HackerRank Python Programming Series.

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Hackerrank by yourself. If you find any difficulty after trying several times, then you can look for solutions.

## XML 2 Find the Maximum Depth in Python Hacker Rank Solution

### Problem

You are given a valid XML document, and you have to print its score. The score is calculated by the sum of the score of each element. For any element, the score is equal to the number of attributes it has.

#### Input Format :

The first line contains N, the number of lines in the XML document.
The next N lines follow containing the XML document.

#### Output Format :

Output a single line, the integer score of the given XML document.

#### Sample Input :

```6
<feed xml:lang='en'>
<title>HackerRank</title>
<subtitle lang='en'>Programming challenges</subtitle>
<updated>2013-12-25T12:00:00</updated>
</feed>
```

```5
```

#### Explanation :

The feed and subtitle tag have one attribute each – lang.
The title and updated tags have no attributes.
The link tag has three attributes – rel, type and href.
So, the total score is 1+1+3 = 5.
There may be any level of nesting in the XML document. To learn about XML parsing, refer here.
NOTE: In order to parse and generate an XML element tree, use the following code:

```>> import xml.etree.ElementTree as etree
>> tree = etree.ElementTree(etree.fromstring(xml))
```

Here, XML is the variable containing the string.
Also, to find the number of keys in a dictionary, use the len function:

```>> dicti = {'0': 'This is zero', '1': 'This is one'}
>> print (len(dicti))
2```

### XML 2 Find the Maximum Depth in Python HackerRank Solutions

```import xml.etree.ElementTree as etree
maxdepth = 0
def depth(elem, level):
global maxdepth
# XML2 - Find the Maximum Depth in Python - Hacker Rank Solution START
if (level == maxdepth):
maxdepth += 1
for child in elem:
depth(child, level + 1)
# XML2 - Find the Maximum Depth in Python - Hacker Rank Solution END
if __name__ == '__main__':
n = int(input())
xml = ""
for i in range(n):
xml =  xml + input() + "\n"
tree = etree.ElementTree(etree.fromstring(xml))
depth(tree.getroot(), -1)
print(maxdepth)```

Disclaimer: The above Problem (XML 2 – Find the Maximum Depth in Python) is generated by Hackerrank but the Solution is Provided by BrokenProgrammers. This tutorial is only for Educational and Learning  purposes.