Restore xml file formatting

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

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

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

    Does anybody know how can I restore xml formatting?

  • Lifetime Qt Champion

    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; 
        QFile outFile("D:/output.xml"); | outFile.WriteOnly);
        QTextStream stream(&outFile);, 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.

  • Lifetime Qt Champion

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

Log in to reply