PHP DOMDocument将节点从文档移动到另一个文档 [英] PHP DOMDocument move nodes from a document to another

查看:85
本文介绍了PHP DOMDocument将节点从文档移动到另一个文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我现在想尝试实现几个小时,而且似乎找不到解决方案,所以我在这里!

OK, I'm trying to achieve this for hours now and can't seem to find a solution so here I am!

我有2个DOMDocument,想要将文档的节点移动到另一个节点。我知道两个文档的结构,并且它们是同一类型(因此合并它们应该没有问题)。

I have 2 DOMDocument and I want to move the nodes of a document to the other one. I know the structure of both documents and they are of the same type (so I should have no problem to merge them).

任何人都可以帮助我吗?如果您需要更多信息,请告诉我。

Anyone can help me? If you need more info let me know.

谢谢!

推荐答案

要将节点复制(或)移动到另一个 DOMDocument ,您必须使用以下命令将节点导入新的 DOMDocument importNode() 。取自手册的示例:

To copy (or) move nodes to another DOMDocument you'll have to import the nodes into the new DOMDocument with importNode(). Example taken from the manual:

$orgdoc = new DOMDocument;
$orgdoc->loadXML("<root><element><child>text in child</child></element></root>");
$node = $orgdoc->getElementsByTagName("element")->item(0);

$newdoc = new DOMDocument;
$newdoc->loadXML("<root><someelement>text in some element</someelement></root>");

$node = $newdoc->importNode($node, true);
$newdoc->documentElement->appendChild($node);

importNode()的第一个参数是节点本身,第二个参数是一个布尔值,指示是否要导入整个节点树。

Where the first parameter of importNode() is the node itself and the second parameter is a boolean that indicates whether or not to import the whole node tree.

这篇关于PHP DOMDocument将节点从文档移动到另一个文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆