我试图使用QXmlStreamReader
解析XML
-file。在下面的代码中,我只从示例xml文件中获取第一个testcase。在
from PyQt4.QtCore import QXmlStreamReader, QFile, QIODevice
class TestcaseReader(object):
def __init__(self, filename):
file = QFile(filename)
file.open(QIODevice.ReadOnly)
self.xml = QXmlStreamReader(file)
while not self.xml.atEnd():
self.xml.readNext()
if self.xml.isStartElement():
if self.xml.name() == "Testcase":
self.parse_testcase()
def parse_testcase(self):
print("Parse Testcase")
while self.xml.readNextStartElement():
if self.xml.name() == "Type":
measurement = self.xml.readElementText()
print("Type: " + measurement)
elif self.xml.name() == "Attributes":
name = self.xml.attributes().value("name")
strname = self.xml.attributes().value("strname")
elementtype = self.xml.attributes().value("type")
value = self.xml.attributes().value("value")
print("Attributes: ", name, strname, elementtype, value)
if __name__ == "__main__":
print("XML Reader")
xml = TestcaseReader("test.xml")
这是我的XML文件:
^{pr2}$在分析来自Testcases
的第一个Testcase
之后,QXmlStreamReader返回它在末尾,因此停止进一步的解析。如何从xml文件中读取所有测试用例?在
当数据QXmlStreamReader以增量方式读取数据时,并非所有数据都可以在QIODevice的缓冲区中使用。从慢速设备(如网络插座)读取数据时尤其如此,但从本地文件读取时也可能发生这种情况。在
阅读更多关于如何处理"Incremental parsing" section of the QXmlStreamReader documentation中的数据块的信息。在
另外,您的XML是无效的,它应该读为}。E、 g.,对于第一个:
<Attributes ... />
,而不是{QXmlStreamReader的error()、errorString()、errorLine()和errorColumn()应该提供调试此类问题所需的所有信息。(最好检查错误并正确报告)。在
相关问题 更多 >
编程相关推荐