PDA

View Full Version : How to append an xml file



athulms
29th August 2011, 07:46
i have created a xml named Score.xml at my current application path.


dir=new QDir(dir->currentPath());
QFile file(dir->filePath("Score.xml"));
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
if (file.open(QIODevice::WriteOnly))
{
QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
xmlWriter->setDevice(&file);
xmlWriter->writeStartDocument();
xmlWriter->writeStartElement("Score");
xmlWriter->writeEndElement();
xmlWriter->writeEndDocument();
file.close();
}
}

then on each run i have to append the xml.

dir=new QDir(dir->currentPath());
QFile file(dir->filePath("Score.xml"));
if (file.open(QIODevice::Append))
{
QXmlStreamWriter* xmlWriter = new QXmlStreamWriter();
xmlWriter->setDevice(&file);
xmlWriter->writeStartDocument();
QMapIterator<QString, QString> i(rank);
QMapIterator<QString, QString> j(name);
QMapIterator<QString, QString> k(time);
i.next();
j.next();
k.next();
xmlWriter->writeStartElement("Player");
xmlWriter->writeAttribute(i.key(), i.value());
xmlWriter->writeAttribute(j.key(), j.value());
xmlWriter->writeAttribute(k.key(), k.value());
xmlWriter->writeEndElement();
xmlWriter->writeEndDocument();
file.close();
}


I obtained the xml as
<?xml version="1.0" encoding="UTF-8"?><Score/>
<?xml version="1.0" encoding="UTF-8"?><Player rank="7" name="Athul" time="01:03"/>
<?xml version="1.0" encoding="UTF-8"?><Player rank="8" name="Sajan" time="02:09"/>


But i need to append the text inside the score tag like.


<?xml version="1.0" encoding="UTF-8"?><Score><Player rank="7" name="Athul" time="01:03"/><Player rank="8" name="Sajan" time="02:09"/></Score>

wysota
29th August 2011, 07:56
You need to read and re-write the whole file, not only the appended part.

athulms
29th August 2011, 08:05
k sir. i will try