gpt4 book ai didi

javascript - 如何在 Javascript 中克隆 XML 文档?

转载 作者:数据小太阳 更新时间:2023-10-29 01:48:03 26 4
gpt4 key购买 nike

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

我试过

var newDocument = myDocument.cloneNode(true);

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

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

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

最佳答案

您可以执行类似以下操作来克隆 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);

关于javascript - 如何在 Javascript 中克隆 XML 文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9711046/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com