[英]How to add node in existing xml using php simpleXML?
I have a xml file like the bellow, and I need to add a new node with some child node and attribute. 我有一个像bellow一样的xml文件,我需要添加一个带有一些子节点和属性的新节点。
<custscales>
<custscale sclNo="1" type="lin">
<scaleName>Custom Scale Lin</scaleName>
<jsfunc>custLin</jsfunc>
</custscale>
<custscale sclNo="2" type="map">
<scaleName>Custome Scale Map</scaleName>
<jsfunc>custMap</jsfunc>
</custscale>
<custscale sclNo="3" type="pol">
<scaleName>Custome Scale Pol</scaleName>
<jsfunc>custPol</jsfunc>
</custscale>
<custscale sclNo="4" type="tbl1">
<scaleName>Custome Scale Table</scaleName>
<jsfunc>custTbl1</jsfunc>
</custscale>
</custscales>
Now I want a new custscale node as bellow in my existing xml file: 现在我想在我现有的xml文件中显示一个新的custscale节点:
<custscale sclNo="5" type="tbl1">
<scaleName>Custome Scale New</scaleName>
<jsfunc>custTbl1</jsfunc>
</custscale>
Use addChild()
and addAttribute()
: 使用
addChild()
和addAttribute()
:
$xml = simplexml_load_string($x); // assume XML in $x
$cs = $xml->addChild('custscale','');
$cs->addAttribute('sclNo','5');
$cs->addChild('scaleName','Some Name');
// add other attributes and child-nodes
see it working: http://codepad.viper-7.com/Y13JbS 看它工作: http : //codepad.viper-7.com/Y13JbS
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.