I'm trying to parse XML
-file using QXmlStreamReader
. With following code I only get the first testcase from the sample xml file.
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")
Here is my XML file:
<?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>
After parsing the first Testcase
from Testcases
QXmlStreamReader returns it is at the end and therefore stops further parsing. How can I read all testcases from the xml file?
As the data QXmlStreamReader reads the data incrementally, not all data might be available in the QIODevice's buffer. That's particularly the case when reading data from a slow device, e.g. a network socket, but can also happen when reading from local files.
Read more about how to handle data arriving in chunks that in the "Incremental parsing" section of the QXmlStreamReader documentation.
Also, your XML is invalid, it should read <Attributes ... />
instead of <Attributes ...>
. E.g., for the first one:
<Attributes name="testattr1" strname="Testattribute 1" type="float" value="1.0"/>
QXmlStreamReader's error(), errorString(), errorLine() and errorColumn() should give you all the information needed to debug such issues. (and it's good practice to check for errors and report them properly anyway).