栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

以下是在其他一些SimpleXMLElement之后插入新的SimpleXMLElement的函数。由于使用SimpleXML不可能直接做到这一点,因此它在幕后使用了一些DOM类/方法来完成工作。

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 = new SimpleXMLElement('<root><nodeA/><nodeA/><nodeA/><nodeC/><nodeC/><nodeC/></root>');// New element to be inserted$insert = new SimpleXMLElement("<nodeB/>");// Get the last nodeA element$target = current($sxe->xpath('//nodeA[last()]'));// Insert the new element after the last nodeAsimplexml_insert_after($insert, $target);// Peek at the new XMLecho $sxe->asXML();

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



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/377379.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号