Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP SimpleXML:在特定位置插入节点_Php_Xml_Simplexml - Fatal编程技术网

PHP SimpleXML:在特定位置插入节点

PHP SimpleXML:在特定位置插入节点,php,xml,simplexml,Php,Xml,Simplexml,假设我有XML: <root> <nodeA /> <nodeA /> <nodeA /> <nodeC /> <nodeC /> <nodeC /> </root> 如何在As和Cs之间插入“nodeB”?在PHP中,最好是通过SimpleXML?比如: <root> <nodeA /> <nodeA /> <nod

假设我有XML:

<root>
  <nodeA />
  <nodeA />
  <nodeA />
  <nodeC />
  <nodeC />
  <nodeC />
</root>

如何在As和Cs之间插入“nodeB”?在PHP中,最好是通过SimpleXML?比如:

<root>
  <nodeA />
  <nodeA />
  <nodeA />
  <nodeB />
  <nodeC />
  <nodeC />
  <nodeC />
</root>

以下是一个函数,用于在其他SimpleXMLElement之后插入新的SimpleXMLElement。由于SimpleXML无法直接实现这一点,因此它在幕后使用一些类/方法来完成工作

function simplexml_insert_after(SimpleXMLElement $insert, SimpleXMLElement $target)
{
    $target_dom = dom_import_simplexml($target);
    $insert_dom = $target_dom->ownerDocument->importNode(dom_import_simplexml($insert), true);
    if ($target_dom->nextSibling) {
        return $target_dom->parentNode->insertBefore($insert_dom, $target_dom->nextSibling);
    } else {
        return $target_dom->parentNode->appendChild($insert_dom);
    }
}
以及如何使用的示例(针对您的问题):

$sxe=新的SimpleXMLElement(“”);
//要插入的新元素
$insert=新的SimpleXMLElement(“”);
//获取最后一个nodeA元素
$target=current($sxe->xpath('//nodeA[last()]');
//在最后一个节点A之后插入新元素
simplexml_insert_后面($insert,$target);
//看看新的XML
echo$sxe->asXML();

如果您想/需要解释这是如何工作的(代码相当简单,但可能包含外国概念),请提问。

萨拉特的回答确实对我有所帮助,但由于我使用了SimpleXMLElement的addChild方法,我寻求了一种解决方案,以使作为第一个孩子插入孩子更加透明。解决方案是采用基于DOM的功能并将其隐藏在SimpleXMLElement的子类中:

class SimpleXMLElementEx extends SimpleXMLElement
{
    public function insertChildFirst($name, $value, $namespace)
    {
        // Convert ourselves to DOM.
        $targetDom = dom_import_simplexml($this);
        // Check for children
        $hasChildren = $targetDom->hasChildNodes();

        // Create the new childnode.
        $newNode = $this->addChild($name, $value, $namespace);

        // Put in the first position.
        if ($hasChildren)
        {
            $newNodeDom = $targetDom->ownerDocument->importNode(dom_import_simplexml($newNode), true);
            $targetDom->insertBefore($newNodeDom, $targetDom->firstChild);
        }

        // Return the new node.
        return $newNode;
    }
}
毕竟,SimpleXML允许指定要使用的元素类:

$xml = simplexml_load_file($inputFile, 'SimpleXMLElementEx');

现在,您可以在任何元素上调用insertChildFirst以将新子元素作为第一个子元素插入。该方法以SimpleXML元素的形式返回新元素,因此其用法类似于addChild。当然,创建一个insertChild方法很容易,它允许指定一个确切的元素来插入之前的项,但是因为我现在不需要它,所以我决定不这样做。

我不确定您是否可以。从文档中可以看出,SimpleXML是用来进行非常简单的操作的。我是否可以将Cs存储在其他地方,删除它们,添加B,然后从临时位置重新添加Cs?我只是不太精通PHP……是我,还是simplexml_insert_中的$sxe参数从未使用过?不是你,为什么回答问题时不开心呢?:)是的,为什么不呢谢谢你的回答,b.t.w.我冒昧地用它来建造我自己的。:)
$xml = simplexml_load_file($inputFile, 'SimpleXMLElementEx');