[Qt-interest] QXmlSreamReader
Sebastian Schöbinger
sebastianlaptop at inode.at
Tue Apr 28 13:32:03 CEST 2009
hi
thank you for your answer (I couldn't write back earlier, because I had
no Computer :( )
It works good, but my xml file looks like this:
<title lang="en">German-English Dictionary"</title>
and the program just shows me
title
langen
, but I also want to see "German-English Dictionary"
how is that "codeable" ? ;)
Sebastian
> Hi,
>
>
>> I have the book "C++ GUI Programming with Qt 4" and I want to make a
>> program, which reads den Strings out of the .xml file, during the runtime.
>>
>> In the book, they load the file in the main.cpp like this:
>>
>> int main [int argc, char *argv[])
>> {
>> QApplication app(argc, argv);
>> QStringList args = QApplication::arguments();
>> ...
>> QTreeWidget
>> ...
>> XmlStreamReader reader(&treeWidget);
>> for (int i = 1; i < args.count(); i++)
>> reader.readFile(args[i]);
>> return app.exec();
>> }
>>
>> during the runtime I can't use the command
>>
>> reader.readFile(&file);
>>
>> does the commang reader.setDevice(&file); do the same or where can I
>> write the command readFile to get the program working.
>>
>>
>
> If you are using XmlStreamReader you need to use readNext(...) instead of
> readFile(...). The following code shows all the tags in an xml file:
>
> QFile xmlFile("test.xml");
> if(!xmlFile.open(QIODevice::ReadOnly | QIODevice::Text))
> return false;
>
> QXmlStreamReader xmlStream(&xmlFile);
> while(!xmlStream.atEnd())
> {
> xmlStream.readNext();
> if(xmlStream.isStartDocument())
> {
> qDebug("Start Document: ");
> }
> if(xmlStream.isEndDocument())
> {
> qDebug("End Document: ");
> }
> if(xmlStream.isStartElement())
> {
> qDebug("Start Element: ");
> qDebug() << xmlStream.name().toString();
> qDebug("Start Element attributes: ");
> QXmlStreamAttributes attributes = xmlStream.attributes();
> foreach(QXmlStreamAttribute a, attributes)
> {
> qDebug() << a.name().toString() << a.value().toString();
> }
> qDebug("Start Element text: ");
> qDebug() << xmlStream.text().toString();
> }
> if(xmlStream.isEndElement())
> {
> qDebug("End Element: ");
> qDebug() << xmlStream.name().toString();
> qDebug() << xmlStream.text().toString();
> }
> }
> if(xmlStream.hasError())
> {
> qDebug("error");
> return -1;
> }
> return 0;
>
>
>
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3674 bytes
Desc: S/MIME Cryptographic Signature
Url : http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090428/b69f9fb6/attachment.bin
More information about the Qt-interest-old
mailing list