Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Error while saving XML file in Windows

Error while saving XML file in Windows

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xtingray
    wrote on last edited by xtingray
    #1

    Hi,

    I am writing a Qt application that saves its data as an XML file. The saving process is really simple, a QPushButton triggers the save() slot described below:

    void save()
    {
        QFile documentFile(projectDir.path() + "/document.xml");
        if (documentFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream ts(&documentFile);
            QDomDocument doc;
            doc.appendChild(project->saveAsXml(doc));
            ts << doc.toString();
            documentFile.close();
        } 
    }  
    

    The method "saveAsXml()" returns a QDomElement object with the whole XML structure of the document. I have been testing this code in Unix/Mac systems without any problem, I mean, the document is always saved successfully.
    Nevertheless, in Windows systems I have been detecting a terrible issue: the XML file is getting corrupted after the saving process. The document files got a huge size (several MB) because the inner structure of the XML file is repeated and repeated hundreds of times with no apparent reason.

    I'm pretty sure that this issue only occurs on Windows systems (using exactly the same source code that I use in Unix). So, my question is: what should I do to fix the problem in Windows? I appreciate any advice about it.


    Qt Developer

    1 Reply Last reply
    0
    • Paul ColbyP Offline
      Paul ColbyP Offline
      Paul Colby
      wrote on last edited by
      #2

      Hi @xtingray, that does sound nasty.

      This line looks suspiciously recursive to me, and may be subject to subtle differences in compiler optimisations:

      doc.appendChild(project->saveAsXml(doc));
      

      Can you try re-writing that as two separate statements? (just to see if its the cause of the issue).

      Cheers.

      X 1 Reply Last reply
      0
      • Paul ColbyP Paul Colby

        Hi @xtingray, that does sound nasty.

        This line looks suspiciously recursive to me, and may be subject to subtle differences in compiler optimisations:

        doc.appendChild(project->saveAsXml(doc));
        

        Can you try re-writing that as two separate statements? (just to see if its the cause of the issue).

        Cheers.

        X Offline
        X Offline
        xtingray
        wrote on last edited by
        #3

        @Paul-Colby Thank you for your comment. I was checking the implementation of the method "saveAsXml()" and effectively, it is recursive.
        I could try to change the way it works right now, but it will take me some time because it is huge. Maybe I will do it in the middle term.

        Anyway, I can't help to keep asking: why this code works so well in Unix, but so bad in Windows if it is the same implementation? I guess the memory/process management is different between platforms and maybe this can cause some issues, but not sure at all.


        Qt Developer

        kshegunovK 1 Reply Last reply
        0
        • X xtingray

          @Paul-Colby Thank you for your comment. I was checking the implementation of the method "saveAsXml()" and effectively, it is recursive.
          I could try to change the way it works right now, but it will take me some time because it is huge. Maybe I will do it in the middle term.

          Anyway, I can't help to keep asking: why this code works so well in Unix, but so bad in Windows if it is the same implementation? I guess the memory/process management is different between platforms and maybe this can cause some issues, but not sure at all.

          kshegunovK Offline
          kshegunovK Offline
          kshegunov
          Moderators
          wrote on last edited by kshegunov
          #4

          @xtingray said:

          I could try to change the way it works right now, but it will take me some time because it is huge. Maybe I will do it in the middle term.

          I think you should regardless. QDomDocument keeps the whole structure in memory and the overhead is significant.

          I guess the memory/process management is different between platforms and maybe this can cause some issues, but not sure at all.

          It is different, and Linux makes many (but not all) things better, that is topped by the fact that the MS compiler is not known for its good code generation. However, such conclusions are purely speculative and ultimately superficial, so I think it's not productive even guessing about it. My suggestion would be to directly write down the XML structure through a QTextStream instance, or even better to use the QXmlStreamWriter class.

          Kind regards.

          Read and abide by the Qt Code of Conduct

          X 1 Reply Last reply
          0
          • kshegunovK kshegunov

            @xtingray said:

            I could try to change the way it works right now, but it will take me some time because it is huge. Maybe I will do it in the middle term.

            I think you should regardless. QDomDocument keeps the whole structure in memory and the overhead is significant.

            I guess the memory/process management is different between platforms and maybe this can cause some issues, but not sure at all.

            It is different, and Linux makes many (but not all) things better, that is topped by the fact that the MS compiler is not known for its good code generation. However, such conclusions are purely speculative and ultimately superficial, so I think it's not productive even guessing about it. My suggestion would be to directly write down the XML structure through a QTextStream instance, or even better to use the QXmlStreamWriter class.

            Kind regards.

            X Offline
            X Offline
            xtingray
            wrote on last edited by
            #5

            @kshegunov Thank you for the advice. I am looking for saving memory consumption in my implementation, so I will follow your recommendations :)


            Qt Developer

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved