PDA

View Full Version : problem in creating xml file.



Niamita
6th September 2011, 09:02
Hi all
I am creating a .xml file and write in it but the contents are not written to it. I am coding like this

const int Indent = 4;
QDomDocument doc;
QDomElement root = doc.createElement("doc");
QDomElement quote = doc.createElement("quote");
QDomElement translation = doc.createElement("translation");
QDomText latin = doc.createTextNode("Ars longa vita brevis");
QDomText english = doc.createTextNode("Art is long, life is short");
doc.appendChild(root);
root.appendChild(quote);
root.appendChild(translation);
quote.appendChild(latin);
translation.appendChild(english);
QFile *file = new QFile("E:\test.txt");
if(!file->open(QIODevice::WriteOnly))
qDebug()<<"error";
QTextStream ts(file);
QDomNode xmlNode = doc.createProcessingInstruction("xml","version=\"1.0\" encoding=\"ISO-8859-1\"");
doc.insertBefore(xmlNode, doc.firstChild());
ts << doc.toString();
doc.save(ts, Indent);
file->close();

Please tell me what i am doing wrong, i am not able to view my file.
Thanks for help.

Added after 6 minutes:

I solved the problem.
Sorry.

UASlaX
6th September 2011, 15:18
Can make better use of QXmlStreamWriter?


QDomDocument doc("mydocument");
QFile file("mydocument.xml");
if (!file.open(QIODevice::ReadOnly))
return;
if (!doc.setContent(&file)) {
file.close();
return;
}
file.close();
It's example from help of QDomDocument class. You need to execute setContent.