如何用SimpleXMLElement PHP替换XML节点 [英] How to replace XML node with SimpleXMLElement PHP

查看:209
本文介绍了如何用SimpleXMLElement PHP替换XML节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下XML(字符串1):

I have the following XML (string1):

<?xml version="1.0"?>
<root>
   <map>
      <operationallayers>
         <layer label="Security" type="feature" visible="false" useproxy="true" usePopUp="all" url="http://stackoverflow.com"/>
      </operationallayers>
   </map>
</root>

我有一段XML(string2):

And I have this piece of XML (string2):

<operationallayers>
    <layer label="Teste1" type="feature" visible="false" useproxy="true" usePopUp="all" url="http://stackoverflow.com"/>
    <layer label="Teste2" type="dynamic" visible="false" useproxy="true" usePopUp="all" url="http://google.com"/>
</operationallayers>

我使用funcion函数simplexml_load_string将它们都导入各自的var:

I used the funcion simplexml_load_string to import both to the respectives var:

$xml1 = simplexml_load_string($string1);
$xml2 = simplexml_load_string($string2);

现在,我想将string1的节点"operationallayers"替换为string2的节点"operationallayers",但是如何?

Now, I want to replace the node 'operationallayers' of the string1 for the node 'operationallayers' of the string2, but how?

SimpleXMLElement类没有像DOM那样的方法'replaceChild'.

The class SimpleXMLElement does not have a method 'replaceChild' like the DOM has.

推荐答案

类似于 SimpleXML:附录中概述的内容一棵树到另一棵树 ,您可以将这些节点导入到DOMDocument中,因为在编写时:

Similar to what has been outlined in SimpleXML: append one tree to another you can import those nodes into DOMDocument because as you write:

类SimpleXMLElement没有像DOM这样的方法'replaceChild'."

因此,当您导入DOM时,您可以使用它们:

So when you import into DOM you can use those:

$xml1 = simplexml_load_string($string1);
$xml2 = simplexml_load_string($string2);

$domToChange = dom_import_simplexml($xml1->map->operationallayers);
$domReplace  = dom_import_simplexml($xml2);
$nodeImport  = $domToChange->ownerDocument->importNode($domReplace, TRUE);
$domToChange->parentNode->replaceChild($nodeImport, $domToChange);

echo $xml1->asXML();

哪个会提供以下输出(未美化):

Which gives you the following output (non-beautified):

<?xml version="1.0"?>
<root>
   <map>
      <operationallayers>
    <layer label="Teste1" type="feature" visible="false" useproxy="true" usePopUp="all" url="http://stackoverflow.com"/>
    <layer label="Teste2" type="dynamic" visible="false" useproxy="true" usePopUp="all" url="http://google.com"/>
</operationallayers>
   </map>
</root>

此外,您还可以执行此操作,并将操作添加到SimpleXMLElement中,以便轻松包装它.这是通过从SimpleXMLElement扩展而来的:

Additionally you can then take this and add the operation to your SimpleXMLElement so that it's easily wrapped. This works by extending from SimpleXMLElement:

/**
 * Class MySimpleXMLElement
 */
class MySimpleXMLElement extends SimpleXMLElement
{
    /**
     * @param SimpleXMLElement $element
     */
    public function replace(SimpleXMLElement $element) {
        $dom     = dom_import_simplexml($this);
        $import  = $dom->ownerDocument->importNode(
            dom_import_simplexml($element),
            TRUE
        );
        $dom->parentNode->replaceChild($import, $dom);
    }
}

用法示例:

$xml1 = simplexml_load_string($string1, 'MySimpleXMLElement');
$xml2 = simplexml_load_string($string2);

$xml1->map->operationallayers->replace($xml2);

相关: 在SimpleXML中,如何添加现有的SimpleXMLElement作为子元素? .

Related: In SimpleXML, how can I add an existing SimpleXMLElement as a child element?.

上次我在Stackoverflow上扩展SimpleXMLElement是在 读取并获取XML属性的值"的答案中 问题.

Last time I extended SimpleXMLElement on Stackoverflow was in an answer to the "Read and take value of XML attributes" question.

这篇关于如何用SimpleXMLElement PHP替换XML节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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