Qt with Cascades UI Examples Documentation


XML Stream Lint Example



The XML Stream Lint example provides a simple UI to select an XML file that gets validated.

The specified file is parsed using an QXmlStreamReader object and written to a QByteArray using an QXmlStreamWriter object. If the file does not contain a well-formed XML document or the use of namespaces in the document is incorrect, a description of the error signaled to the UI.

Basic Operation

Reading XML is handled by an instance of the QXmlStreamReader class, which operates on the input file object; writing is handled by an instance of QXmlStreamWriter operating on the output buffer object:

        // Create a XML stream reader that reads the XML document from the file
        QXmlStreamReader reader(&file);

        // Create a XML stream reader that writes an XML document to the memory buffer
        QXmlStreamWriter writer(&buffer);

The work of parsing and rewriting the XML is done in a while loop, and is driven by input from the reader:

        // Now let the XML stream reader parse the XML document token by token ...
        while (!reader.atEnd()) {

            // ... and update the result property if an error occurs ...
            if (reader.error()) {
                m_result = tr("Error: %1 in file %2 at line %3, column %4.\n")
                             .arg(reader.errorString(), fileName, QString::number(reader.lineNumber()), QString::number(reader.columnNumber()));
                emit resultChanged();

If more input is available, the next token from the input file is read and parsed. If an error occurred, information is signaled to the UI.

            } else {
                // ... otherwise let the XML stream writer write out this token

For valid input, the writer is fed the current token from the reader, and this is written to the output buffer that was specified when it was constructed.

When there is no more input, the loop terminates, and the UI is informed about the result and the output.