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. QXmlStreamWriter overriding data in xml
QtWS25 Last Chance

QXmlStreamWriter overriding data in xml

Scheduled Pinned Locked Moved General and Desktop
xmlserial
8 Posts 2 Posters 4.6k 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.
  • E Offline
    E Offline
    Espresso
    wrote on last edited by
    #1

    I am receiving data from serial port which I have managed to display in a gui (have implemented signals and slots). Now i want to save the data in xml, which I have managed to do but there is a slight problem.

    The data is received and displayed in realtime but when it comes to storing it, the previous values are lost and the xml is updated with the new value. for example, lets say, value = 2,30,4..... xml only keeps one value whereas I want to store all the values in xml, not just one.

    readData(); //reads serial data and stores it in QByteArray.
    saveData(QByteArray data); // saves data into an xml (only the current value, not all the values)

    '''QT C++
    void saveData(QByteArray data)
    {
    //creating a document to write xml
    QDomDocument document;
    QDomElement base = document.createElement(data);
    document.appendChild(base);
    QFile file("/home/data.xml");
    if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
    {
    qDebug()<< "file could not be opened";
    }
    else
    {
    QXmlStreamWriter xmlWrite(&file);

    xmlWrite.setAutoFormatting(true);
    xmlWrite.writeStartDocument();
    xmlWrite.writeCDATA(data);
    xmlWrite.writeEndDocument();
    file.close();}
    

    '''

    P.S: I have spent some time but couldn't get far due to my limited knowledge. Any help or insight would be appreciated.

    Thanks for reading.

    Cheers,
    Karim

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

      Hi and welcome to devnet,

      What you are currently doing is just over-writing your file each time. What you should do is read it, update the XML and then write it again.

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

      1 Reply Last reply
      1
      • E Offline
        E Offline
        Espresso
        wrote on last edited by
        #3

        Thanks for your reply SGaist. Is it possible to store a list into an xml. N.B, its not a list of objects rather it is a list of some raw data. I am aware of QVector(adjacent to memory etc) but saving it or a list into xml is the point here.

        for instance

        """
        saveData(QList data)
        {
        QFile file("path");
        QXmlStreamWriter xmlWrite(&file);
        xmlWrite.writeCDATA(data);
        }
        """

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

          Sure it possible but not with the code your proposed.

          You have to loop over your data to put them at the right place.

          I'd recommend taking the time studying how to create a good xml document to represent your data.

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

          E 1 Reply Last reply
          1
          • SGaistS SGaist

            Sure it possible but not with the code your proposed.

            You have to loop over your data to put them at the right place.

            I'd recommend taking the time studying how to create a good xml document to represent your data.

            E Offline
            E Offline
            Espresso
            wrote on last edited by
            #5

            @SGaist Thanks. You are right that I would need to loop over the data for correct formatting. As things stand, I am unable to loop properly over the data. Reason being, data is continuously increasing (reading from serial port). I am appending the values to QByteArray.

            Here is the pseudo-code

            ’’’
            readData()
            {
            data = //read data.
            static QString storeData;
            storeData.append(data);

            //using QXmlStreamWriter instead of QDom
            QXmlStreamWriter stream(&file);
            ...
            xml.writeTextElement("value", storeData); //Creates an xml with all the values in between just one element tag
            //i.e. <value> x x x x x x x x x x </value> where each x is a different value
            }
            '''

            I have tried to store the values first into an array but that wouldn't work either. If I try to store the values (lets say 10 values), to an array, somehow the values are not being stored in that array.
            QString arr[10] = storeData; // arr is empty.

            Thanks for your support.

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

              Are you receiving null characters ? If so, you should rather stay with QByteArray

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

              1 Reply Last reply
              0
              • E Offline
                E Offline
                Espresso
                wrote on last edited by
                #7

                nope, not receiving null characters. just raw data. As of now, it serves the purpose but I am afraid down the pike, work will be messy.

                For instance, http://www.qtcentre.org/threads/53380-QXmlStreamReader-and-reading-an-XML-with-data-on-multiple-lines

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

                  Sorry, I misunderstood your problem. You don't need to keep all the data in-memory, you can read the file update the xml and write it again.

                  On the other hand, you could also use a simple sqlite database to store your values and generate the xml only when needed.

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

                  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