Results 1 to 14 of 14

Thread: QDomDocument::attribute() failes to read Russian characters

  1. #1
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt3
    Platforms
    Windows

    Question QDomDocument::attribute() failes to read Russian characters

    Hi.

    My problem is this:

    There is a XML-document that I would like to edit using the QDom-classes. This document contains Russian characters. To make sure that the QDom-class can read from this document correctly I saved the file with UTF-8 encoding since - according to Qt-Assistant - this is the endoding that the QDom classes assume if you do not make another statement.

    QDomDocument::setContent() is successfull. Now there is an attribute in my XML-file and the value of this attribute contains the Russian characters. In my text editor the characters are displayed correctly but the method attribute("name of attribute") seems to return a row of "?"s instead of the actual value.

    My first impression was that the debugger just can´t show the Russian characters and shows "?" instead. But if I assign the return value from attribute() to a QLabel then I see the "?"s, too.

    What is wrong here?

    Qt-Version: 3.3.7
    Visual C++ 6.0

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomDocument::attribute() failes to read Russian characters

    Are you sure the file was saved in UTF-8 encoding? If you receive question marks, it usually means that something didn't manage to read the file correctly. And furthermore I'm not sure if you can use non-ascii characters in attributes. But check the encoding first.

  3. #3
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QDomDocument::attribute() failes to read Russian characters

    I used the editor Notepad 2 (http://www.flos-freeware.ch/notepad2.html) to view the xml-document. There is an item in the file menu to set the encoding to UTF-8. Then I saved the document. The first line says:

    <?xml version="1.0" encoding="UTF-8"?>

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomDocument::attribute() failes to read Russian characters

    I'm asking about the file encoding, not xml encoding. Try opening the file with other editors and see if you get characters that look like unicode. Also try loading your file into QTextEdit and making sure you convert from Utf8 using QString::fromUtf8(). The minimal app would look a bit like:
    Qt Code:
    1. int main(int argc, char **argv){
    2. QApplication app(argc, argv);
    3. QFile f("myfile.xml");
    4. f.open(QFile::ReadOnly);
    5. QString contents = QString::fromUtf8(f.readAll().constData());
    6. te.setPlainText(contents);
    7. te.show();
    8. return app.exec();
    9. }
    To copy to clipboard, switch view to plain text mode 

    See if that gives you proper russian characters.

  5. #5
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QDomDocument::attribute() failes to read Russian characters

    Yes, if I load the file into your mini app then QTextEdit shows me the Russian characters

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomDocument::attribute() failes to read Russian characters

    So now try changing your xml so that the russian characters are not contained in attributes. For example instead of:
    xml Code:
    1. <doc>
    2. <tag attr="value"/>
    3. </doc>
    To copy to clipboard, switch view to plain text mode 
    make:
    xml Code:
    1. <doc>
    2. <tag>
    3. <attr>value</attr>
    4. </tag>
    5. </doc>
    To copy to clipboard, switch view to plain text mode 

    And see if the problem persists.

  7. #7
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QDomDocument::attribute() failes to read Russian characters

    I wrote a tiny app that has a dialog and two labels. The first label should show the Russian characters as text of a node and the second label should show the characters as value of an attribute. Result: Both texts are not correct.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomDocument::attribute() failes to read Russian characters

    Could you attach the questionable xml file here? I'd like to take a look at it.

  9. #9
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QDomDocument::attribute() failes to read Russian characters

    It seems I paid attention on the XML structure too much. If I read the file and assign the pure content to the label (no xml parsing) then the characters are not correctly shown as well.

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomDocument::attribute() failes to read Russian characters

    Could you attach the file here?

  11. #11
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QDomDocument::attribute() failes to read Russian characters

    Surprisingly I had to change the extension to "txt" to make the upload work.
    Attached Files Attached Files

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomDocument::attribute() failes to read Russian characters

    This seems to work fine for me:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtCore>
    3. #include <QtXml>
    4. #include <QtDebug>
    5.  
    6. int main(int argc, char **argv){
    7. QApplication app(argc, argv);
    8. QFile f("Test.txt");
    9. f.open(QFile::ReadOnly);
    10. qDebug() << doc.setContent(f.readAll());
    11. QDomElement elem = doc.documentElement();
    12. QDomElement txt = elem.firstChildElement("Test1");
    13. te.setPlainText(txt.text());
    14. QDomElement txt2 = elem.firstChildElement("Test2");
    15. te.append(txt2.attribute("wert"));
    16. te.show();
    17. return app.exec();
    18. }
    To copy to clipboard, switch view to plain text mode 
    In both cases I receive "Срочное выключение установки задействовано". It might be because for my system local Qt encoding is utf-8. In your case I think you should add a header to the xml file specifying the utf-8 encoding.

  13. #13
    Join Date
    Oct 2007
    Posts
    7
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: QDomDocument::attribute() failes to read Russian characters

    According to your include statements you seem to use Qt 4. I had to modify your code to make it compile with my Qt 3 but now it works. Now I have to find out what I did wrong???

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomDocument::attribute() failes to read Russian characters

    Could you show us your non-working code?

Similar Threads

  1. Problem at time compilation in traslation of language
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 22nd May 2007, 15:18

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.