我正在编写的 Python 应用程序中使用 libxml2,并尝试运行一些测试代码来解析 XML 文件。该程序从 Internet 下载 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() ,但它仍然抛出错误。