Qt World Summit: Submit your Presentation

[Solved] XML coded in UTF-8 on Linux

  • Hello,
    I have written app which reads XML with QXMLQuery and using XQuery recieving data. But I am fighting for 2 days with encoding. Code:

    @QFile sourceDocument( ":/xml/data.xml", this );
    sourceDocument.open( QIODevice::ReadOnly );
    m_query.bindVariable( "inputDocument", &sourceDocument );
    QByteArray outArray;
    QBuffer buffer( &outArray );

    buffer.open( QIODevice::ReadWrite );
    m_query.setQuery( query_str );
    m_query.evaluateTo( &buffer );
    QString result = QString::fromUtf8( outArray );@

    Result string contains some numbers instead of special chatacters...
    Please, can somebody tell me, how to process that UTF-8 XML ??


  • You're opening your file as binary, which I don't understand why, just wanted to point that out.

    Then anyway, remember that for special characters, the stand ASCII decoding doesn't apply anymore. You have to to use 2 byte characters


    Which are supported by standard C++ with char_w and other similar containers. Please read about that. For example, if I want to save some data with Arabic language, I have to save the whole file with 2 byte decoding.


  • I know that I need to use 2byte char.. I am sure that I dont need to use char_w with Qt, QString is working with UTF-16 by default.. and QFile does not mean that I am opening it as binary... I need to specify right codec for XML parser when it evaluates the query, because he messes up.. I am able to decode that XML into QString via QFile without corruption, but QXMLQuery does not support that I will pass the document as data, it wants to access the data itself.. and than it messes up encoding

  • Qt already handles UTF very well. There is no need whatsoever to use char_w here.

    @adam.bogocz: I'm sorry, I don't know the answer to your actual question.

  • What's the result if you evaluate to a QString directly, instead of a QByteArray?

  • Hi Volker, passing QString into evaluate works well, conding is kept :) Thanks !

  • Don't forget to mark the topic as solved then, thanks!

Log in to reply