Restore xml file formatting



  • Hi,
    I have following problem:
    I get the xml file from QNetworkReply.

       QFile newFile( "tmp.xml" );
       newFile.open( QIODevice::WriteOnly );
       newFile.write( reply->readAll() );
       newFile.close();
    

    The content is ok, but xml file is not formatted(all in one line).

    Does anybody know how can I restore xml formatting?


  • Qt Champions 2017

    Hi
    Use Qt Xml classes to read it in again ( or parse from reply->readAll())
    and write it to file formatted.
    You can give it directly to
    bool QDomDocument::setContent(const QString & text,..

    Something like ( not compiled. will have errors :)
    And please add error checking.

    void format(QByteArray xml)
    {
        QDomDocument doc; 
        doc.setContent(xml);
        QFile outFile("D:/output.xml");
        outFile.open(outFile.Text | outFile.WriteOnly);
        QTextStream stream(&outFile);
        doc.save(stream, DomNode::EncodingFromDocument);
    }
    


  • @mrjj said in Restore xml file formatting:

    QDomDocument doc;

    Ok, it helped me a lot, but I suggest to use QDomNode::EncodingFromDocument option as a second parameter in save function.

    Thanks for help.


  • Qt Champions 2017

    @Kaluss
    Super.
    Fixed, thank you. :)
    ( magic values are evil )


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.