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

Read xml file

Scheduled Pinned Locked Moved Solved General and Desktop
7 Posts 3 Posters 2.4k Views
  • 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.
  • I Offline
    I Offline
    ivanicy
    wrote on 15 Jun 2017, 11:20 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
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 15 Jun 2017, 11:53 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

      V 1 Reply Last reply 15 Jun 2017, 13:18
      1
      • I Offline
        I Offline
        ivanicy
        wrote on 15 Jun 2017, 12:49 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
        • S SGaist
          15 Jun 2017, 11:53

          Hi,

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

          QFile
          QXmlStreamReader

          V Offline
          V Offline
          VRonin
          wrote on 15 Jun 2017, 13:18 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
          • I Offline
            I Offline
            ivanicy
            wrote on 16 Jun 2017, 07:36 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
            • V Offline
              V Offline
              VRonin
              wrote on 16 Jun 2017, 07:55 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

              I 1 Reply Last reply 16 Jun 2017, 09:45
              1
              • V VRonin
                16 Jun 2017, 07:55

                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)

                I Offline
                I Offline
                ivanicy
                wrote on 16 Jun 2017, 09:45 last edited by
                #7

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

                1 Reply Last reply
                0

                5/7

                16 Jun 2017, 07:36

                • Login

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