PDA

View Full Version : Blank file using QXmlStreamWriter



sgmurphy19
14th November 2007, 20:50
I get a blank file using the following code. Can anyone shed some light on why? I know it is writeable as it creates the file and goes into the if statement.

Thanks in advanced!

QString fileName = QFileDialog::getSaveFileName(0, "Save File", "", "XML Files ( *.xml)");

file = new QFile(fileName);
if(file->open(QIODevice::WriteOnly | QIODevice::Text))
{
file->close();
xmlWriter = new QXmlStreamWriter();
xmlWriter->setDevice(file);

xmlWriter->writeStartDocument();
xmlWriter->writeDTD("<!DOCTYPE xbel>");
xmlWriter->writeStartElement("xbel");
xmlWriter->writeAttribute("version", "1.0");
xmlWriter->writeTextElement("Uri", "name", "This is Text");
xmlWriter->writeEndDocument();
}

jpn
14th November 2007, 20:53
What if you don't close the file before writing to it?

PS. You don't have to allocate everything on the heap..

sgmurphy19
14th November 2007, 21:02
Ignore that line. I put that in to test if anything would change if I did close the file first. Either way it comes out blank :(

jpn
14th November 2007, 21:14
Works for me:


#include <QtGui>
#include <QtXml>

int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QString fileName = QFileDialog::getSaveFileName(0, "Save File", "", "XML Files ( *.xml)");
if (!fileName.isNull())
{
QFile file(fileName);
if (file.open(QIODevice::WriteOnly | QIODevice::Text))
{
QXmlStreamWriter xmlWriter(&file);
xmlWriter.writeStartDocument();
xmlWriter.writeDTD("<!DOCTYPE xbel>");
xmlWriter.writeStartElement("xbel");
xmlWriter.writeAttribute("version", "1.0");
xmlWriter.writeTextElement("Uri", "name", "This is Text");
xmlWriter.writeEndDocument();
}
}
return 0;
}

sgmurphy19
14th November 2007, 21:45
Thanks jpn,
When I changed my code to constructor statements it worked. Not sure why it didn't like the method approach.