Qt xmlWriter/xmlReader

不打扰是莪最后的温柔 提交于 2019-12-10 10:56:26

问题


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


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/6898567/qt-xmlwriter-xmlreader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!