PDA

View Full Version : QDomDocument::attribute() failes to read Russian characters



Simplix
10th October 2007, 10:03
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!

wysota
10th October 2007, 10:34
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.

Simplix
10th October 2007, 11:42
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"?>

wysota
10th October 2007, 11:47
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:

int main(int argc, char **argv){
QApplication app(argc, argv);
QTextEdit te;
QFile f("myfile.xml");
f.open(QFile::ReadOnly);
QString contents = QString::fromUtf8(f.readAll().constData());
te.setPlainText(contents);
te.show();
return app.exec();
}

See if that gives you proper russian characters.

Simplix
10th October 2007, 12:22
Yes, if I load the file into your mini app then QTextEdit shows me the Russian characters

wysota
10th October 2007, 12:26
So now try changing your xml so that the russian characters are not contained in attributes. For example instead of:
<doc>
<tag attr="value"/>
</doc>
make:
<doc>
<tag>
<attr>value</attr>
</tag>
</doc>

And see if the problem persists.

Simplix
10th October 2007, 14:15
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.

wysota
10th October 2007, 14:25
Could you attach the questionable xml file here? I'd like to take a look at it.

Simplix
10th October 2007, 14:32
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.

wysota
10th October 2007, 14:34
Could you attach the file here?

Simplix
10th October 2007, 14:38
Surprisingly I had to change the extension to "txt" to make the upload work.

wysota
10th October 2007, 14:51
This seems to work fine for me:

#include <QtGui>
#include <QtCore>
#include <QtXml>
#include <QtDebug>

int main(int argc, char **argv){
QApplication app(argc, argv);
QTextEdit te;
QFile f("Test.txt");
f.open(QFile::ReadOnly);
QDomDocument doc;
qDebug() << doc.setContent(f.readAll());
QDomElement elem = doc.documentElement();
QDomElement txt = elem.firstChildElement("Test1");
te.setPlainText(txt.text());
QDomElement txt2 = elem.firstChildElement("Test2");
te.append(txt2.attribute("wert"));
te.show();
return app.exec();
}
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.

Simplix
10th October 2007, 15:00
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???

wysota
10th October 2007, 15:38
Could you show us your non-working code?