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. Append XML using QXmlStreamWriter
Forum Updated to NodeBB v4.3 + New Features

Append XML using QXmlStreamWriter

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 4 Posters 864 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.
  • M Offline
    M Offline
    michalt38
    wrote on 18 Dec 2019, 16:31 last edited by michalt38
    #1

    Hello,

    I have a following example XML:

    <YEAR NUM="2000">
       <MONTH NUM="2">
          <DAY NUM="10">
             <VAL>10</VAL>
             <VAL>14</VAL>
          </DAY>
       </MONTH>
       <MONTH NUM="5">
          <DAY NUM="23">
             <VAL>13</VAL>
             <VAL>16</VAL>
          </DAY>
       </MONTH>
    </YEAR>
    <YEAR NUM="2019">
       <MONTH NUM="6">
          <DAY NUM="13">
             <VAL>14</VAL>
             <VAL>12</VAL>
          </DAY>
       </MONTH>
       <MONTH NUM="11">
          <DAY NUM="18">
             <VAL>13</VAL>
             <VAL>16</VAL>
          </DAY>
       </MONTH>
    </YEAR>
    

    What I want is to append this XML adding a new VAL element for YEAR NUM=2019, MONTH NUM=11 and DAY NUM=18. How can I do that using QXmlStreamWriter?

    1 Reply Last reply
    0
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 18 Dec 2019, 17:22 last edited by
      #2

      Hi
      Must it be QXmlStreamWriter ?
      Im asking as its very good for parsing huge files but for updating it on the fly - it gets a bit more hardcore
      as you need to write to a new file since the original is open for reading and you can't just write to it on the fly. (AFAIK)

      Using QDomDocument for updating should be much simpler as shown here.

      https://stackoverflow.com/questions/45814463/modify-an-xml-file-qxmlstreamreader-writer

      1 Reply Last reply
      4
      • M Offline
        M Offline
        michalt38
        wrote on 24 Dec 2019, 19:11 last edited by michalt38
        #3

        Thanks for the answear. What is the best method for saving the data to XML in a real time and not holding the whole document in the memory?

        J 1 Reply Last reply 25 Dec 2019, 11:37
        0
        • C Offline
          C Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on 24 Dec 2019, 20:32 last edited by
          #4

          @michalt38 said in Append XML using QXmlStreamWriter:

          What is the best method for saving the data to XML in a real time and not holding the whole document in the memory?

          QXmlStreamWriter

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          3
          • M michalt38
            24 Dec 2019, 19:11

            Thanks for the answear. What is the best method for saving the data to XML in a real time and not holding the whole document in the memory?

            J Online
            J Online
            JonB
            wrote on 25 Dec 2019, 11:37 last edited by JonB
            #5

            @michalt38
            Further to @Christian-Ehrlicher's answer, if you want to append something to that XML file without holding in memory (e.g. via QDomDocument), that means you will need to read it via QXmlStreamReader and copy to the output via QXmlStreamWriter as you go, inserting/appending your new XML element at the appropriate point. And since you are not reading the whole document into memory, you must not write directly to the file you are reading from: you will need to write your new output to a new, temporarily-named file, and only at conclusion close each stream reader/writer and rename the output file back to the original one if that is what you want to do.

            1 Reply Last reply
            1

            1/5

            18 Dec 2019, 16:31

            • Login

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