Blank file using QXmlStreamWriter
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();
}
Re: Blank file using QXmlStreamWriter
What if you don't close the file before writing to it?
PS. You don't have to allocate everything on the heap..
Re: Blank file using QXmlStreamWriter
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 :(
Re: Blank file using QXmlStreamWriter
Works for me:
Code:
#include <QtGui>
#include <QtXml>
int main(int argc, char *argv[])
{
QString fileName
= QFileDialog::getSaveFileName(0,
"Save File",
"",
"XML Files ( *.xml)");
if (!fileName.isNull())
{
{
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;
}
Re: Blank file using QXmlStreamWriter
Thanks jpn,
When I changed my code to constructor statements it worked. Not sure why it didn't like the method approach.