문제

I'm taking my first steps with Qt4.8.6 under Debian 7 at the moment and I really like it! But now I need a little help with this issue:

My application connects to a server and triggers a data request. XML data is then sent back in one or multiple TCP packets to my application, where it is assembled into a complete "message", which is nothing else then a well-formed XML document containing the requested data.

Receiving the data works flawlessly, but now I want to display the received XML data in a nicely formed way for debug purposes etc. The problem: I do have the XML data in a simple QString and I don't want to start beautifying it by hand with my own routines. I've seen that there is a XmlStreamWriter that offers auto formatting. That sounds nice! But how can I "feed" it with my XML data and let it output the formatted data into another QString?

void MainWindow::displayMessage(QString message)
{
    QString formattedOutput;

    QXmlStreamReader xmlreader(message);

    QXmlStreamWriter xmlwriter(&formattedOutput);
    xmlwriter.setAutoFormatting(true);

    ResponseTextEdit->append(formattedOutput);
}

I'm somehow missing the link: the XML data is already present, it can also be read by the xmlreader. The xmlwriter is configured to write into the QString formattedOutput and is also set to auto-format the output. But how can I get my XML data into the xmlwriter!? Or is this a completely wrong approach and there's an easier way to output XML from a string in a tidied-up shape?

Thanks in advance for your help!

도움이 되었습니까?

해결책

The XML stream readers & writers are a bit too low-level for your purpose; you'd have to copy manually from the reader to the writer.

You're better off constructing a DOM document from your string and saving it. Something like this:

void MainWindow::displayMessage(QString message)
{
    QString formattedOutput;

    QDomDocument doc;
    doc.setContent(message, false);

    QTextStream writer(&formattedOutput);
    doc.save(writer, 4);  //or whatever indentation you want

    ResponseTextEdit->append(formattedOutput);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top