Read xml file
Solved
General and Desktop
-
Hello.
I am trying to read a xml file but I can't get. This is the xml file:
<?xml version="1.0" encoding="UTF-8"?> <SceneData version="v1.0"> <GraphicsItemList> <MaxNode xCoord="559" yCoord="231"/> <RoiItem> <RoiItemText xCoord="160" yCoord="44"/> <RoiItemMaxText xCoord="295" yCoord="174"/> <RoiItemMinText xCoord="268" yCoord="170"/> <RoiItemROI xCoord="170" yCoord="59"/> <RoiItemFirst xCoord="170" yCoord="59"/> <RoiItemSecond xCoord="376" yCoord="229"/> <RoiItemMax xCoord="295" yCoord="174"/> <RoiItemMin xCoord="268" yCoord="170"/> </RoiItem> <PointItem1> <PointItem1Text xCoord="451" yCoord="36"/> <PointItem1Point xCoord="471" yCoord="56"/> </PointItem1> <PointItem2> <PointItem2Text xCoord="558" yCoord="120"/> <PointItem2Point xCoord="578" yCoord="140"/> </PointItem2> <LineItem> <LineItemText xCoord="186" yCoord="279"/> <LineItemLine xCoord="0" yCoord="0"/> <LineItemFirst xCoord="186" yCoord="279"/> <LineItemSecond xCoord="445" yCoord="420"/> </LineItem> <MinNode xCoord="0" yCoord="410"/> </GraphicsItemList> </SceneData>
Any idea to how to start the reading? Thank you very much!
-
-
-
With the file that I showed before and this read code:
QXmlStreamReader reader(&file); if (reader.readNextStartElement()) { if (reader.name() == "SceneData") { while (reader.readNextStartElement()) { if (reader.name() == "GraphicsItemList") { while (reader.readNextStartElement()) { qDebug() << reader.name(); if (reader.name() == "MaxNode") { maxNodeX = reader.attributes().value("xCoord").toString(); maxNodeY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } if (reader.name() == "MinNode") { qDebug() << "MinNode"; qDebug() << "minNodeX: " << reader.readElementText(); minNodeX = reader.attributes().value("xCoord").toString(); minNodeY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } if (reader.name() == "RoiItem") { qDebug() << "RoiItem"; while (reader.readNextStartElement()) { if (reader.name() == "RoiItemText") { qDebug() << " RoiItemText"; roiTextX = reader.attributes().value("xCoord").toString(); roiTextY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } if (reader.name() == "RoiItemMaxText") { qDebug() << " RoiItemMaxText"; roiMaxTextX = reader.attributes().value("xCoord").toString(); roiMaxTextY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } if (reader.name() == "RoiItemMinText") { qDebug() << " RoiItemMinText"; roiMinTextX = reader.attributes().value("xCoord").toString(); roiMinTextY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } if (reader.name() == "RoiItemFirst") { qDebug() << " RoiItemFirst"; roiFirstX = reader.attributes().value("xCoord").toString(); roiFirstY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } if (reader.name() == "RoiItemSecond") { qDebug() << " RoiItemSecond"; roiSecondX = reader.attributes().value("xCoord").toString(); roiSecondY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } if (reader.name() == "RoiItemMax") { qDebug() << " RoiItemMax"; roiMaxX = reader.attributes().value("xCoord").toString(); roiMaxY = reader.attributes().value("yCoord").toString(); } if (reader.name() == "RoiItemMin") { qDebug() << " RoiItemMin"; roiMinX = reader.attributes().value("xCoord").toString(); roiMinY = reader.attributes().value("yCoord").toString(); reader.readNextStartElement(); } } } if (reader.name() == "PointItem1") { qDebug() << "PointItem1"; while (reader.readNextStartElement()) { if (reader.name() == "PointItem1Text") { qDebug() << " PointItem1Text"; point1TextX = reader.attributes().value("xCoord").toString(); point1TextY = reader.attributes().value("yCoord").toString(); } if (reader.name() == "PointItem1Point") { qDebug() << " PointItem1Point"; point1PointX = reader.attributes().value("xCoord").toString(); point1PointY = reader.attributes().value("yCoord").toString(); } } } if (reader.name() == "PointItem2") { qDebug() << "PointItem2"; while (reader.readNextStartElement()) { if (reader.name() == "PointItem2Text") { qDebug() << " PointItem2Text"; point2TextX = reader.attributes().value("xCoord").toString(); point2TextY = reader.attributes().value("yCoord").toString(); } if (reader.name() == "PointItem2Point") { qDebug() << " PointItem2Point"; point2PointX = reader.attributes().value("xCoord").toString(); point2PointY = reader.attributes().value("yCoord").toString(); } } } if (reader.name() == "LineItem") { qDebug() << "LineItem"; while (reader.readNextStartElement()) { if (reader.name() == "LineItemText") { qDebug() << " LinteItemText"; lineTextX = reader.attributes().value("xCoord").toString(); lineTextY = reader.attributes().value("yCoord").toString(); } if (reader.name() == "LineItemFirst") { qDebug() << " LinteItemFirst"; lineFirstX = reader.attributes().value("xCoord").toString(); lineFirstY = reader.attributes().value("yCoord").toString(); } if (reader.name() == "LineItemSecond") { qDebug() << " LinteItemSecond"; lineSecondX = reader.attributes().value("xCoord").toString(); lineSecondY = reader.attributes().value("yCoord").toString(); } } } } } reader.raiseError(QObject::tr("Incorrect file")); } } }
I only get the MaxNode and RoiItemText, RoiItemMaxText and RoiItemMinText. The other values are empty.
What am I doing wrong?
Thank you very much
-
Do not nest the while, do only 1 big loop.
See: https://github.com/VSRonin/FXhelper/blob/master/FXhelperApp/PortfoliosTab.cpp#L1249 (that reads https://github.com/VSRonin/FXhelper/blob/master/FXhelperApp/Settings.xml)