Skip to content
Advertisement

Extract XML Attribute-Python

I am new to Python & trying to extract XML attributes. Below is the code that I tried.

JavaScript

I wanted to extract ‘name’ and ‘address’ from XML. But when I run the above code nothing is printed. What is my mistake?

Regards, Mayank Pande

Advertisement

Answer

Namespaces dawg, namespaces! You can be damn sure that when Jay-Z rapped about having 99 problems, having to deal with XML with namespaces was definitely one of them!

See Parsing XML with Namespaces

For the body tag, its namespace is http://schemas.xmlsoap.org/soap/envelope/, checkVatResponse‘s is urn:ec.europa.eu:taxud:vies:services:checkVat:types, and both name and address‘s are urn:ec.europa.eu:taxud:vies:services:checkVat:types, which they inherit off their parent, checkVatResponse.

So, you can explicitly search for an element including its namespace, like so:

JavaScript

Or you can ignore it with the wildcard character:

JavaScript

Try this:

JavaScript

Output:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement