添加新的XML根节点 [英] Adding in new XML root node

查看:110
本文介绍了添加新的XML根节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在以下XML中添加新的根节点

I need to add in a new root node to the following XML

<?xml version="1.0"?>
<unit>
<source> 
<id>ANCH02</id> 
<uri>http://www.hamiltonisland.biz/tabid/339/Default.aspx</uri> 
</source>
</unit>

成为

<?xml version="1.0"?>
        <units>
        <unit>
        <source> 
        <id>ANCH02</id> 
        <uri>http://www.hamiltonisland.biz/tabid/339/Default.aspx</uri> 
        </source>
        </unit>
        </units>

我该怎么做?看起来SimpleXMLElement似乎没有此功能。我还查看了此DomNode示例 http://php.net/manual/zh/domnode .insertbefore.php ,但是它似乎无法添加新的根节点。

How could I do this? It doesn't seem like SimpleXMLElement has this functionality. I have also looked at this DomNode example http://php.net/manual/en/domnode.insertbefore.php but it doesnt seem to be able to add in a new root node.

推荐答案

这似乎工作

$units = $dom->createElement('units');
$units->appendChild($dom->documentElement);
$dom->appendChild($units);

演示

这篇关于添加新的XML根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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