Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Read xml file
Qt 6.11 is out! See what's new in the release blog

Read xml file

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 3.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ivanicyI Offline
    ivanicyI Offline
    ivanicy
    wrote on last edited by
    #1

    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!

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      It depends on what you want to do with the content of that file:

      QFile
      QXmlStreamReader

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      VRoninV 1 Reply Last reply
      1
      • ivanicyI Offline
        ivanicyI Offline
        ivanicy
        wrote on last edited by
        #3

        I want to save the information in variables and then emit a signal with this information.

        1 Reply Last reply
        0
        • SGaistS SGaist

          Hi,

          It depends on what you want to do with the content of that file:

          QFile
          QXmlStreamReader

          VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by VRonin
          #4

          Then:

          @SGaist said in Read xml file:

          QXmlStreamReader

          http://www.walletfox.com/course/qxmlstreamreaderexample.php

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1
          • ivanicyI Offline
            ivanicyI Offline
            ivanicy
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            • VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by VRonin
              #6

              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)

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              ivanicyI 1 Reply Last reply
              1
              • VRoninV VRonin

                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)

                ivanicyI Offline
                ivanicyI Offline
                ivanicy
                wrote on last edited by
                #7

                @VRonin Thank you very much!! It works succesfully!

                1 Reply Last reply
                0

                • Login

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • Users
                • Groups
                • Search
                • Get Qt Extensions
                • Unsolved