Categories
Python Answers

How to parse XML with namespace in Python via ‘ElementTree’?

Spread the love

Sometimes, we want to parse XML with namespace in Python via ‘ElementTree’.

In this article, we’ll look at how to parse XML with namespace in Python via ‘ElementTree’.

How to parse XML with namespace in Python via ‘ElementTree’?

To parse XML with namespace in Python via ‘ElementTree’, we can use ther findall method.

For instance, we write

namespaces = {'owl': 'http://www.w3.org/2002/07/owl#'} 
root.findall('owl:Class', namespaces)

to call findall with 'owl:Class' and the namespaces dict with all the namespaces that we want to extract from the root XML object.

Conclusion

To parse XML with namespace in Python via ‘ElementTree’, we can use ther findall method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *