QProcess > QXmlStreamReader problems (premature end of document)



  • Hi,

    I have a command line program that outputs about 100Mb worth of (well-formed) XML.
    If I redirect the output to a XML file, I can parse it without problems with QXmlStreamReader.

    However I would like to avoid having to create the file first and stream the output directly using QProcess.

    Here's my approach;

    @
    QProcess process;
    QXmlStreamReader reader(process);
    process.start("/usr/bin/xmlprog",QProcess::ReadOnly)
    while(!reader.atEnd() && !reader.hasError())
    {
    qDebug() << reader.lineNumber()
    }
    @

    This snippet would start the process and print every line number to stdout. The problem is that this code only works every now and then - i.e. it usually fails with a 'premature end of document' error, though sometimes it appears to work at first and then suddenly stop with the same error, and it gets exponentially slower the longer it runs.

    So how to handle this properly? Maybe I need to use a buffer? Any help?

    Thanks for reading!


  • Moderators

    i guess you have to listen to QProcess::finished() signal. If the program exited without an error you should read the stdout of the program.
    I think the cases where it won't work are that you (QXmlStreamReader) try to read the data it has output but hasn't completely parsed the XML yet.

    So start the QProcess first, when it has finished read the stdout and then pass it to the QXmlStreamReader.


Log in to reply
 

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