AttributeError: xmlNode实例没有属性'isCountNode

0 投票
1 回答
1900 浏览
提问于 2025-04-15 11:14

我在写一个Python应用程序时使用libxml2,想要运行一些测试代码来解析一个XML文件。这个程序会从网上下载一个XML文件并进行解析。不过,我遇到了一个问题。

使用以下代码:

xmldoc = libxml2.parseDoc(gfile_content)

droot = xmldoc.children         # Get document root
dchild = droot.children         # Get child nodes

while dchild is not None:
        if dchild.type == "element":
                print "\tAn element with ", dchild.isCountNode(), "child(ren)"
                print "\tAnd content", repr(dchild.content)
        dchild = dchild.next
xmldoc.freeDoc();

...这段代码是参考了在XML.com上的这篇文章中的示例。我在尝试在Python 2.4.3(CentOS 5.2的版本)上运行这段代码时,收到了以下错误。

Traceback (most recent call last):
  File "./xml.py", line 25, in ?
    print "\tAn element with ", dchild.isCountNode(), "child(ren)"
AttributeError: xmlNode instance has no attribute 'isCountNode'

我现在有点卡住了。

补充:我还想提一下,我也尝试了IsCountNode(),但还是出现了错误。

1 个回答

3

isCountNode 应该改成 "lsCountNode"(注意是小写的 "L")

撰写回答