【问题标题】:Is it necessary to make QDomElement created by the same QDomDocument to write XML?是否需要让同一个QDomDocument创建的QDomElement才能写XML?
【发布时间】:2013-03-23 09:55:16
【问题描述】:

我使用 QDomDocument 来编写 XML 文档。
但是在我的 dom 树中,有些节点是使用 docA 创建的,有些是使用 docB 创建的。

QDomElement parentNode = docA.CreateElement("name");//created by docA
QDomElement childNode = docB.CreateElement("value");//created by docB
parentNode.appendChild(childNode);//in onr tree

还有:

QTextStream out(&file);
docA.save(out, Indent);//docA created the root QDomElement
                       //write the file using docA      

那么有可能像这样将整个树写成 XML 吗?

【问题讨论】:

    标签: c++ qt dom io tree


    【解决方案1】:

    您应该避免这种情况,因为如果 docA 超出范围,如果 docB 仍在使用,事情就会开始出错。我相信你的提议在技术上会起作用,直到发生这种情况,但图书馆似乎旨在阻止它。

    但是,有一个函数 QDomDocument::importNode() 可能是您想要的。你可以这样做:

    docAParent.appendChild( docA.importNode( docBNode, true ) );
    

    布尔参数控制是否进行深层复制。

    查看文档:http://qt-project.org/doc/qt-4.8/qdomdocument.html#importNode

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-25
      • 2013-06-17
      • 1970-01-01
      • 2014-02-21
      • 1970-01-01
      • 2020-03-14
      相关资源
      最近更新 更多