[Qt-interest] Have QXmlStreameWriter write the encoding information

Andreas Pakulat apaku at gmx.de
Wed Jun 3 23:08:42 CEST 2009


On 03.06.09 11:04:04, Lingfa Yang wrote:
> Andreas Pakulat wrote:
> > On 02.06.09 23:01:37, Lingfa Yang wrote:
> >> What you want is a processing instruction line, so you should call 
> >> writeProcessingInstruction(), i.e.
> >>     xml.writeProcessingInstruction("xml", "version=\"1.0\" 
> >> encoding=\"UTF-8\"");
> >>     
> >
> > That will produce
> >
> > <?xml version="1.0"?><?xml version="1.0" encoding="UTF-8"?>
> >
> > so definetly not what he wants.
> >
> > If you're writing into a QString there's simply no point in adding an
> > encoding, because the encoding is dictated by QString.
> >   
> Try it, Andreas. It does work:-)

In fact, Qt's XmlStreamReader won't read the result back in, see this
example:

,----[ main.cpp ]-
| #include <QtCore>
| #include <QDebug>
| #include <QtXml>
| 
| int main(int argc, char** argv)
| {
|     QString tmp;
|     QXmlStreamWriter w(&tmp);
|     w.setCodec(QTextCodec::codecForName("ISO-8859-1"));
|     w.writeStartDocument("1.0");
|     w.writeProcessingInstruction("xml","version=\"1.0\" encoding=\"iso-8859-1\"");
|     w.writeStartElement("test");
|     w.writeCharacters(QString::fromUtf8("är"));
|     w.writeEndElement();
|     w.writeEndDocument();
| 
|     QFile file("test.xml");
|     file.open( QIODevice::WriteOnly );
|     QTextStream s(&file);
|     s.setCodec(QTextCodec::codecForName("ISO-8859-1"));
|     s << tmp;
|     file.close();
| 
|     file.open( QIODevice::ReadOnly );
|     QXmlStreamReader r(&file);
|     while( !r.atEnd()) {
|         QXmlStreamReader::TokenType t = r.readNext();
|         qDebug() << t;
|         if( t == QXmlStreamReader::Invalid ) {
|             qDebug() << "invalid token, error:" << r.errorString();
|         }
|     }
| 
| 
| }
`----

It'll produce an encoding error. Its too late right now to look up what the
XML spec says about that, but I'm pretty sure it specifies that the
encoding is to be read from the first <?xml?> PI in the document,
because later PI's can possibly already be encoded in some form.

Andreas

-- 
Your temporary financial embarrassment will be relieved in a surprising manner.



More information about the Qt-interest-old mailing list