Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 2

Qt-interest Archive, March 2002
QDom and XML


Message 1 in thread

Hello,

I have an XML file beginning with
<?xml version="1.0" encoding="ISO-8859-1"?>
No I created a QDomDocument and used setContents to set its contents
to the contents of the file. How can I retrieve the encoding, so that I can
use QTextCodec to decode the text contained in nodes? especially for
other encodings that iso-8859-1?

Regards,
Volker augustin


Message 2 in thread

Hi,

> I have an XML file beginning with
> <?xml version="1.0" encoding="ISO-8859-1"?>
> No I created a QDomDocument and used setContents to set its contents
> to the contents of the file. How can I retrieve the encoding, so that I can
> use QTextCodec to decode the text contained in nodes? especially for
> other encodings that iso-8859-1?

The text in the nodes (and all other strings) are QStrings -- i.e.
already unicode. The parsing that via the setContent() should already
take care of the right encoding. There is no need to use a QTextCodec to
get the strings.

Rainer

-- 
 [ signature omitted ] 

Message 3 in thread

> > The text in the nodes (and all other strings) are QStrings -- i.e.
> > already unicode. The parsing that via the setContent() should already
> > take care of the right encoding. There is no need to use a QTextCodec to
> > get the strings.
> 
> This problem came up in the KNewsticker applet of KDE 3.0. In
> kdenetwork/knewsticker/common/xmlnewsaccess.cpp there is the following
> (shortened) code:
> 
> QDomDocument domDoc;
> domDoc.setContent(QCString(data));
>   QDomNodeList items = domDoc.elementsByTagName(QString::fromLatin1("item"));
>   QDomNode itemNode;
>   QString headline;
>   for (unsigned int i = 0; i < items.count(); i++) {
>   itemNode = items.item(i);
>   headline = 
> itemNode.namedItem(QString::fromLatin1("title")).toElement().text();
> }

One problem in your code is that you use the QDomDocument::setContent()
overload that takes a QCString. In this case the input is considered to
be UTF8 (which it isn't!) -- in general, one should not use this
function, since the XML document could be in an encoding that is not
representable as a C string anyway (e.g. in UTF16).

> The initial data here is a QByteArray. [...]

So why do you convert it into a QCString instead of using the QByteArray
directly?

> Replacing the QCString in line #2 with QString [...]

This is in your case as bad as using a QCString, since the encoding is
ignored as well.

> [...]
> For japanese http://slashdot.jp/slashdot.rdf. [...]

The file I get from this URL does not seem to be an XML file at all.

Rainer

-- 
 [ signature omitted ]