【问题标题】:Importing XML to DOM tree in Qt在 Qt 中将 XML 导入 DOM 树
【发布时间】:2011-04-20 03:48:30
【问题描述】:

我在 Mac OS X 上使用 Qt 4.7,并且我有一个包含 XML 文件路径的 QString。我想将该文件导入 DOM 树并将数据作为成员变量存储到类中。做这个的最好方式是什么?

我一直在查看QtXml 文档,但找不到从QXml* 类转换为QDom* 类的明确方法。

【问题讨论】:

    标签: c++ qt qtxml


    【解决方案1】:

    我认为您无需费心使用 QXml* 类来遍历 DOM。

    QDomDocument 类有一个 setContent() 方法,可以获取打开的 QFile。

    There's a code sample 在 QDomDocument 文档的“详细信息”部分。

    QDomDocument doc("mydocument");
    QFile file("mydocument.xml");
    if (!file.open(QIODevice::ReadOnly))
        return;
    if (!doc.setContent(&file)) {
        file.close();
        return;
    }
    file.close();
    
    // print out the element names of all elements that are direct children
    // of the outermost element.
    QDomElement docElem = doc.documentElement();
    
    QDomNode n = docElem.firstChild();
    while(!n.isNull()) {
        QDomElement e = n.toElement(); // try to convert the node to an element.
        if(!e.isNull()) {
            cout << qPrintable(e.tagName()) << endl; // the node really is an element.
        }
        n = n.nextSibling();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-25
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多