Results 1 to 5 of 5

Thread: Blank file using QXmlStreamWriter

  1. #1
    Join Date
    Aug 2007
    Posts
    29
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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();
    }

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default 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..
    J-P Nurmi

  3. #3
    Join Date
    Aug 2007
    Posts
    29
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Blank file using QXmlStreamWriter

    Works for me:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtXml>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication app(argc, argv);
    7. QString fileName = QFileDialog::getSaveFileName(0, "Save File", "", "XML Files ( *.xml)");
    8. if (!fileName.isNull())
    9. {
    10. QFile file(fileName);
    11. if (file.open(QIODevice::WriteOnly | QIODevice::Text))
    12. {
    13. QXmlStreamWriter xmlWriter(&file);
    14. xmlWriter.writeStartDocument();
    15. xmlWriter.writeDTD("<!DOCTYPE xbel>");
    16. xmlWriter.writeStartElement("xbel");
    17. xmlWriter.writeAttribute("version", "1.0");
    18. xmlWriter.writeTextElement("Uri", "name", "This is Text");
    19. xmlWriter.writeEndDocument();
    20. }
    21. }
    22. return 0;
    23. }
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  5. #5
    Join Date
    Aug 2007
    Posts
    29
    Thanks
    8
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default 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.

Similar Threads

  1. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  2. file renaming on windows
    By jdd81 in forum Qt Programming
    Replies: 9
    Last Post: 2nd October 2007, 19:41
  3. Filtering blank spaces while saving file names
    By jyoti kumar in forum Qt Programming
    Replies: 1
    Last Post: 3rd September 2007, 06:53
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Sending Binary File with QFTP
    By nbkhwjm in forum Newbie
    Replies: 2
    Last Post: 7th March 2007, 18:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.