This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Add another element to xml

Tags: None
(comma "," separated)
User avatar
Mar91
Registered Member
Posts
55
Karma
0

Add another element to xml

Wed Aug 11, 2010 4:52 pm
I wrote an xml file with the class QXmlWriter, here is the code:

QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();

xmlWriter->setDevice(&file);

xmlWriter->writeStartDocument();

xmlWriter->writeStartElement("notes");


xmlWriter->writeStartElement("note");

xmlWriter->writeAttribute("data","15/07/2010);

xmlWriter->writeCharacters ("here is the note");

xmlWriter->writeEndElement();

}

xmlWriter->writeEndElement();

xmlWriter->writeEndDocument();
delete xmlWriter;
}
this code works,and this is the result:
<?xml version="1.0" encoding="UTF-8"?>
<Notes>
<note date="mer ago 11 2010" >text note</note>
</Notes>
now the xml file is closed by </Notes> and i can't understand how can i add another element...(i mean <note date="gio ago 12 2010" >text note</note>) before the closing tag.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS

Re: Add another element to xml

Thu Aug 12, 2010 5:01 am
The following code should work.

Code: Select all
// Setup
QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
xmlWriter->setDevice(&file);

// Open document
xmlWriter->writeStartDocument();

// Open <notes> element
xmlWriter->writeStartElement("notes");

// Open <note> element
xmlWriter->writeStartElement("note");
xmlWriter->writeAttribute("date","15/07/2010);
xmlWriter->writeCharacters ("here is the note");
xmlWriter->writeEndElement();
// End <note> element

// Open <note> element
xmlWriter->writeStartElement("note");
xmlWriter->writeAttribute("date","23/12/2010);
xmlWriter->writeCharacters ("note text");
xmlWriter->writeEndElement();
// End <note> element

// End <notes> element
xmlWriter->writeEndElement();

// Close document
xmlWriter->writeEndDocument();

// Cleanup
delete xmlWriter;


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
User avatar
Mar91
Registered Member
Posts
55
Karma
0

Re: Add another element to xml

Thu Aug 12, 2010 11:43 am
Yes, this code works. Imagine now that you want to modify the xml file, the Document has been closed, and i can't add another element..
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS

Re: Add another element to xml

Fri Aug 13, 2010 4:29 am
You will need to read ( parse ) the xml back into your application, then add the data and write it back out again.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]


Bookmarks



Who is online

Registered users: Bing [Bot], blue_bullet, Google [Bot], rockscient, Yahoo [Bot]