![](/img/trans.png)
[英]Selenium find_elements_by_id() doesn't return all elements
[英]XML parsing using QXmlStreamReader doesn't return all elements
我正在尝试使用QXmlStreamReader
解析XML
-file。 使用以下代码,我只从示例xml文件中获取第一个测试用例。
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文件:
<?xml version="1.0" encoding="UTF-8" ?>
<Testcases>
<Testcase>
<Type>Testtype1</Type>
<Attributes name="testattr1" strname="Testattribute 1" type="float" value="1.0">
<Attributes name="testattr2" strname="Testattribute 2" type="str" value="test">
</Testcase>
<Testcase>
<Type>Testtype2</Type>
<Attributes name="testattr1" strname="Testattribute 1" type="float" value="2.0">
<Attributes name="testattr2" strname="Testattribute 2" type="str" value="test">
</Testcase>
</Testcases>
从Testcases
解析第一个Testcase
,QXmlStreamReader返回它的结尾,因此停止进一步解析。 如何从xml文件中读取所有测试用例?
当数据QXmlStreamReader以递增方式读取数据时,并非QIODevice缓冲区中的所有数据都可用。 从慢速设备(例如网络套接字)读取数据时尤其如此,但在从本地文件读取数据时也会发生这种情况。
在QXmlStreamReader文档的“增量解析”部分中,阅读有关如何处理以块为单位的数据的更多信息。
此外,您的XML无效,它应该读取<Attributes ... />
而不是<Attributes ...>
。 例如,对于第一个:
<Attributes name="testattr1" strname="Testattribute 1" type="float" value="1.0"/>
QXmlStreamReader的error(),errorString(),errorLine()和errorColumn()应该为您提供调试此类问题所需的所有信息。 (无论如何,检查错误并正确报告是一种很好的做法)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.