Python parsing XML and my NodeList object don't have len attribute

I am very new to Python and trying to write a program that parses some XML. I have a problem: when I try to call .len()on what I think is a NodeList, I get an error 'NodeList' object has no attribute 'len'. This is really amazing for me because the documentation says:

In addition, the Python DOM interface requires additional support that allows you to use NodeList objects as Python sequences. All NodeList implementations must include len () support.

Here is my code:

import xml.dom.minidom

def testFunction(translationDOM):
    textCollection = translationDOM.getElementsByTagName("onscreen_text")
    for onscreenText in textCollection:
        print textCollection.len()

and then in Main()...

translationDom  = parse(xmlFileName)
testFunction(translationDom)

I don't want to publish all my xml here (its massive), but there are a few blocks similar to:

<onscreen_text>
    <source id="2036" unique_name="blah" should_be_translated="True">
    ....
 </onscreen_text>

Here is the full error text:

  File "trophytool.py", line 155, in <module>
    main()
  File "trophytool.py", line 134, in main
    testFunction(translationDom)
  File "trophytool.py", line 64, in testFunction
    print textCollection.len()
AttributeError: 'NodeList' object has no attribute 'len'

, <onscreen_text>, . ?

+3
2

:

print len(textCollection)

: len , __len__() ( len()), , , len(object).

+1

getElementsByTagName() , . , xml

<school>
   <department />
</school>

"school",

myNode.getElementsByTagName("department")

, getElementsByTagName() ,

myNode.getElementsByTagName("department").item(0)

and it will return a single node instead of a list of nodes with only one node.

+1
source

All Articles