我对Python很陌生,试图编写解析某些XML的程序。我遇到了这个问题,当我尝试拨打.len()
时,我认为它是NodeList,我得到错误'NodeList' object has no attribute 'len'
。这真是令我感到诧异,因为documentation说:在Python中解析XML和我的'NodeList'对象没有属性'len'
此外,Python的DOM接口需要一些额外的支持,以允许使用像Python序列节点列表对象。所有节点列表的实现必须包括LEN()支持
这里是我的代码:
import xml.dom.minidom
def testFunction(translationDOM):
textCollection = translationDOM.getElementsByTagName("onscreen_text")
for onscreenText in textCollection:
print textCollection.len()
然后在Main()
...
translationDom = parse(xmlFileName)
testFunction(translationDom)
我不想发布我的整个xml在这里(其庞大),但有一些块类似于:
<onscreen_text>
<source id="2036" unique_name="blah" should_be_translated="True">
....
</onscreen_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>
标签发现的数量,但事实并非如此。为什么是这样?
尝试'textCollection.length' – Torious 2012-04-15 03:10:37