最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

None type when parsing xml data in Python - Stack Overflow

programmeradmin1浏览0评论

I'd like to be able to parse some info from xml data while uses namespaces. I have been trying to read/follow steps in: .etree.elementtree.html#parsing-xml-with-namespaces

This is a sample of how my xml looks like

When I try going through xml tree using a nested for loop, it looks like my code can read the sub-elements successfully.

def toXML(s):
    xmlRoot= ET.fromstring(s)
    
    for child in xmlRoot:
        print(child.tag)
        for subchild in child:
            print(subchild.tag)
            for subchild in subchild:
                print(subchild.tag)

Output:

{}Body
{}GetRecordingSummaryResponse
{}Summary

Process finished with exit code 0

However, when I try to do this the nice way

    for child in xmlRoot.find('{}Summary'):
        NumberRecordings=child.find('{}NumberRecordings')
        print(NumberRecordings.text)

I get this error:

for child in xmlRoot.find('{}Summary'):
                 ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable

Any idea why my child is None ?

I'd like to be able to parse some info from xml data while uses namespaces. I have been trying to read/follow steps in: https://docs.python./3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces

This is a sample of how my xml looks like

When I try going through xml tree using a nested for loop, it looks like my code can read the sub-elements successfully.

def toXML(s):
    xmlRoot= ET.fromstring(s)
    
    for child in xmlRoot:
        print(child.tag)
        for subchild in child:
            print(subchild.tag)
            for subchild in subchild:
                print(subchild.tag)

Output:

{http://www.w3./2003/05/soap-envelope}Body
{http://www.onvif./ver10/search/wsdl}GetRecordingSummaryResponse
{http://www.onvif./ver10/search/wsdl}Summary

Process finished with exit code 0

However, when I try to do this the nice way

    for child in xmlRoot.find('{http://www.onvif./ver10/search/wsdl}Summary'):
        NumberRecordings=child.find('{http://www.onvif./ver10/schema}NumberRecordings')
        print(NumberRecordings.text)

I get this error:

for child in xmlRoot.find('{http://www.onvif./ver10/search/wsdl}Summary'):
                 ~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: 'NoneType' object is not iterable

Any idea why my child is None ?

Share Improve this question asked 2 days ago most.of.a.sharkmost.of.a.shark 336 bronze badges 3
  • 2 Use .findall(), not .find(). find() returns a single element or None, you can't iterate over either of them. – Barmar Commented 2 days ago
  • 1 It doesn't say that child is None. It says that xmlRoot.find() returns None, and you can't loop over that. – Barmar Commented 2 days ago
  • If you do this xmlRoot.find('.//{http://www.onvif./ver10/search/wsdl}Summary') this should work. – Hermann12 Commented yesterday
Add a comment  | 

1 Answer 1

Reset to default 0

You can find the text values with find(".//prefix:tagname", namespace). No need for interation.

from lxml import etree

xmlFile = "your_xml_file.xml"
root = etree.parse(xmlFile).getroot()
ns = root.nsmap

dataFrom = root.find(".//tt:Data-From", ns).text
print(dataFrom)

dataUntil = root.find(".//tt:DataUntil", ns).text
print(dataUntil)

numberRecords = root.find(".//tt:NumberRecordings", ns).text
print(numberRecords)

Output:

2025-03-19T15:22:10z
2025-03-19T18:30:52Z
1

Or as an shorter alternative search for <tse:Summary> with find() and iterate over the childs:

for elem in root.find(".//tse:Summary", ns):
    print(elem.text)
发布评论

评论列表(0)

  1. 暂无评论