Peter
Peter

Reputation: 213

Qt xmlWriter/xmlReader

I want to write and read an xml file using Qt. Is there a simple example code, which generates an XML file dynamically?

I've found some Qt xml classes, but does anybody know what they are used to and is there an easy example which uses these classes?

QtXml Module (http://doc.qt.nokia.com/latest/qtxml.html)
QXmlStreamReader (http://doc.qt.nokia.com/4.7/qxmlstreamreader.html)
QXmlStreamWriter (http://doc.qt.nokia.com/4.7/qxmlstreamreader.html)

I've written a managed C++ code, but my problem is that this code (using /clr required) does not support IntelliSense in Visual Studio 2010. Now I try to find an alternative. If anybody knows something which has almost the same functions, only using unmanaged code, would be perfect!

Moreover I've found this, but don't really know how to use it: QString to XML in QT

Upvotes: 2

Views: 10275

Answers (2)

Peter
Peter

Reputation: 213

Thanks for your answer!

I've found these links for generating and reading XML files using QT from Nokia:

Writing XML Files: http://developer.nokia.com/community/wiki/Generate_XML_programatically_in_Qt

Reading XML Files: http://www.developer.nokia.com/Community/Wiki/Using_QXmlStreamReader_to_parse_XML_in_Qt

This are the QT classes used, when writing and reading XML files:

http://doc.qt.nokia.com/4.7/qxmlstreamwriter.html

http://doc.qt.nokia.com/4.7/qxmlstreamreader.html

Upvotes: 4

Humberto Pinheiro
Humberto Pinheiro

Reputation: 1174

if you intend to parse small xml files the easiest way is using QDomDocument class, see example below taken from "C++ GUI Programming with Qt4, Second Edition".

QFile file(filename);
QString errorStr;
int errorLine;
int errorColumn;
QDomDocument doc;
if (!doc.setContent(&file, false, &errorStr, &errorLine,
                    &errorColumn)) {
    std::cerr << "Error: Parse error at line " << errorLine << ", "
              << "column " << errorColumn << ": "
              << qPrintable(errorStr) << std::endl;
    return false;
}
QDomElement root = doc.documentElement();
if (root.tagName() != "bookindex") {
    std::cerr << "Error: Not a bookindex file" << std::endl;
    return false;
}

However, the entire xml is kept in memory, so be careful with large xml files.

Upvotes: 1

Related Questions