1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] None type when parsing xml data in Python

Discussão em 'Python' iniciado por Stack, Abril 2, 2025.

  1. Stack

    Stack Membro Participativo

    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.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces

    This is a sample of how my xml looks like [​IMG]

    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.org/2003/05/soap-envelope}Body
    {http://www.onvif.org/ver10/search/wsdl}GetRecordingSummaryResponse
    {http://www.onvif.org/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.org/ver10/search/wsdl}Summary'):
    NumberRecordings=child.find('{http://www.onvif.org/ver10/schema}NumberRecordings')
    print(NumberRecordings.text)


    I get this error:

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


    Any idea why my child is None ?

    Continue reading...

Compartilhe esta Página