【问题标题】:How do I clone a XML document, in Javascript?如何在 Javascript 中克隆 XML 文档?
【发布时间】:2012-03-31 10:45:42
【问题描述】:

在 Javascript 中克隆 XML 文档的最佳方法是什么?

我试过了

var newDocument = myDocument.cloneNode(true);

但这只是返回 null。我也考虑过做

var newNode = myDocument.documentElement.cloneNode(true);

但这对我的目的来说还不够,因为这样新节点的ownerDocument 和以前一样。

【问题讨论】:

    标签: javascript xml dom


    【解决方案1】:

    您可以执行以下操作来克隆 XML 文档:

    var newDocument = oldDocument.implementation.createDocument(
        oldDocument.namespaceURI, //namespace to use
        null,                     //name of the root element (or for empty document)
        null                      //doctype (null for XML)
    );
    var newNode = newDocument.importNode(
        oldDocument.documentElement, //node to import
        true                         //clone its descendants
    );
    newDocument.appendChild(newNode);
    

    【讨论】:

    • 为了更完整,你如何获得newDocument
    猜你喜欢
    • 1970-01-01
    • 2014-08-29
    • 2021-12-11
    • 2019-01-05
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多