PDA

View Full Version : Adding elements with QXmlStreamWriter



hazardpeter
21st August 2009, 15:52
So suppose we have an application that creates and writes and XML file using QXmlStreamWriter, and the XML will look like this:


<xml>

<element>

<atrib>Something</atrib>
<atrib>Something else</atrib>

</element>
<element>

<atrib>Other</atrib>
<atrib>Do not know</atrib>

</element>

</xlm>
What I would neeed now is that other application opens the file and ands another <element> block so the file would look like this:


<xml>
<element>
<atrib>Something</atrib>
<atrib>Something else</atrib>
</element>
<element>
<atrib>Other</atrib>
<atrib>Do not know</atrib>
</element>
<element>
<atrib>New</atrib>
<atrib>New</atrib>
</element>
</xlm>
Any guess

Lykurg
21st August 2009, 19:55
Any guess
Em, read the docs about QXmlStreamWriter?

QXmlStreamWriter stream(&output);
stream.setAutoFormatting(true);
stream.writeStartDocument();
// write your old data.
stream.writeStartElement("element");
stream.writeTextElement("atrib", "New");
stream.writeTextElement("atrib", "New");
stream.writeEndElement();
//...
stream.writeEndDocument();

hazardpeter
21st August 2009, 21:09
I see, I had thought of that. I was just hopping for another way to do this but I don't mind xD. Thanks for the information!!

Lykurg
21st August 2009, 23:08
if you don't want to write all the old data yourself, you can consider using QDomDocument and adding there a new node and then simply save the whole via QDomNode::save().

hazardpeter
22nd August 2009, 12:45
if you don't want to write all the old data yourself, you can consider using QDomDocument and adding there a new node and then simply save the whole via QDomNode::save().

Looks good, I will look in to that, thanks