-1

enter image description here

Im using xml.etree.ElementTree to parse my XML data. I'm trying to get the text value of <Name>

This is my code.

for Content in Zone[0]:
    print(Content.find('Name').text)

It is returning as NoneObject

However, I am able to access the Element using

for Content in Zone[0]:
    print(Content[12].text)

I think I might have found the problem as when I print the tags out, it doesn't display Name and instead it displays {http://schemas.datacontract.org/2004/07/}Name. What is the extra data infront of the tag name?

Exceptions
  • 1,174
  • 2
  • 9
  • 28

1 Answers1

0

Your XML is likely has default namespace -namespace declared with no prefix-. Notice that descendant elements without prefix inherits default namespace implicitly. You can handle default namespace the way you would handle prefixed namespaces; just map a prefix to the namespace URI, and use that prefix along with element name to reference element in namespace :

namespaces = {'d': 'http://schemas.datacontract.org/2004/07/'}
for Content in Zone[0]:
    print(Content.find('d:Name', namespaces).text)
Community
  • 1
  • 1
har07
  • 88,338
  • 12
  • 84
  • 137