开发者

How can I clone nodes between documents in PHP? [duplicate]

开发者 https://www.devze.com 2023-01-07 22:04 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: DOMElement cloning and appending: ‘Wrong Document Error’
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

DOMElement cloning and appending: ‘Wrong Document Error’

I would like to copy all the child nodes of element foo in DOMDocument A into element bar in DOMDocument B. However, using appendChild to do so apparently throws a DOM_WRONG_DOCUMENT_ERR.

Is there a right way of doing this?

XML Document A

<foo>
    <child 开发者_开发知识库/>
    <child />
    <child />
</foo>

XML Document B

<bar>
    <other-child />
    <other-child />
</bar>

Resulting DOMDocument:

<bar>
    <other-child />
    <other-child />
    <child />
    <child />
    <child />
</bar>

The elements should not be assumed to be empty, but are arbitrarily complex.


You will probably want to take a look at DOMDocument::importNode.

This function returns a copy of the node to import and associates it with the current document.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号