【问题标题】:Qt xmlWriter/xmlReaderQt xmlWriter/xmlReader
【发布时间】:2016-06-02 13:58:05
【问题描述】:

我想用 Qt 读写一个 xml 文件。有没有简单的例子代码,动态生成一个XML文件?

我找到了一些 Qt xml 类,但有人知道它们的用途吗?有没有使用这些类的简单示例?

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)

我编写了托管 C++ 代码,但我的问题是此代码(需要使用 /clr)不支持 Visual Studio 2010 中的 IntelliSense。现在我尝试寻找替代方法。如果有人知道具有几乎相同功能的东西,只使用非托管代码,那就完美了!

此外,我找到了这个,但真的不知道如何使用它:QString to XML in QT

【问题讨论】:

    标签: c++ xml qt xml-serialization unmanaged


    【解决方案1】:

    感谢您的回答!

    我找到了使用诺基亚的 QT 生成和读取 XML 文件的这些链接:

    编写 XML 文件: http://developer.nokia.com/community/wiki/Generate_XML_programatically_in_Qt

    读取 XML 文件: http://www.developer.nokia.com/Community/Wiki/Using_QXmlStreamReader_to_parse_XML_in_Qt

    这是编写和读取 XML 文件时使用的 QT 类:

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

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

    【讨论】:

    • 前两个链接坏了。
    【解决方案2】:

    如果您打算解析小的 xml 文件,最简单的方法是使用 QDomDocument 类,请参阅下面的示例,摘自“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;
    }
    

    但是,整个 xml 都保存在内存中,所以要小心处理大型 xml 文件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-17
      • 2012-02-21
      • 2016-06-22
      • 1970-01-01
      • 1970-01-01
      • 2014-01-30
      相关资源
      最近更新 更多