Solved Append XML using QXmlStreamWriter
-
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?
-
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
-
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
-
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?
-
@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
-
@michalt38
Further to @Christian-Ehrlicher's answer, if you want to append something to that XML file without holding in memory (e.g. viaQDomDocument
), that means you will need to read it viaQXmlStreamReader
and copy to the output viaQXmlStreamWriter
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.