我正在使用 org.w3c.dom 来处理一些 xml 文档。我正在使用 jython 2.5.1 来实现它。
我的 xml 文档(EmployeeInfo.xml)的一部分是这样的:
<employees>
<employee id="1">
<name>ABC</name>
<title>Software Engineer</title>
</employee>
<employee id="2">
<name>DEF</name>
<title>Systems Engineer</title>
</employee>
<employee id="3">
<name>GHI</name>
<title>QA Engineer</title>
</employee>
......
</employees>
我用于读取和解析 xml 的 jython 代码如下:
import sys, logging
logging.basicConfig(level=logging.INFO)
from java.io import File
from javax.xml.parsers import DocumentBuilder
from javax.xml.parsers import DocumentBuilderFactory
from org.w3c.dom import Document
from org.w3c.dom import Element
from org.w3c.dom import Node
from org.w3c.dom import NodeList
// ... some code
file = "C:/Users/Adminstrator/Doc/EmployeeInfo.xml"
doc = File(file)
if doc.exists():
docFactory = DocumentBuilderFactory.newInstance()
docFactory.setNamespaceAware(True)
docBuilder = docFactory.newDocumentBuilder()
if doc.endswith(".xml"):
logging.info(" -- Reading " + doc)
employeeDoc = docBuilder.parse(doc)
if employeeDoc != None:
employees = employeeDoc.getElementsByTagNameNS("*","employee")
if employees != None:
for employee in employees:
logging.info(employee.getChildNodes().getLength())
else:
logging.warn("Failed to get the employee from " + doc)
else:
logging.warn("Failed to parse the document " + doc)
else:
logging.warn("Failed to find the specified document" + doc + ", please check the path!")
当我运行这个脚本时,出现了一个错误:
TypeError: 'org.apache.xerces.dom.DeepNodeListImpl' object is not iterable
参考线:
for employee in employees:
似乎它自动将“员工”视为 jython 的 NodeList 而不是 org.w3c.dom.NodeList ...
我在网上搜索了这个问题,但我对这个问题知之甚少......这里有人可以帮我解决这个问题吗?提前致谢!