QXmlStreamWriter overriding data in xml
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)
void saveData(QByteArray data)
//creating a document to write xml
QDomElement base = document.createElement(data);
if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
qDebug()<< "file could not be opened";
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.
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.
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.
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.
@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
data = //read data.
static QString storeData;
//using QXmlStreamWriter instead of QDom
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 = storeData; // arr is empty.
Thanks for your support.
Are you receiving null characters ? If so, you should rather stay with QByteArray
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.
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.