I have a xml file like the bellow, and I need to add a new node with some child node and attribute.
<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:
<custscale sclNo="5" type="tbl1">
<scaleName>Custome Scale New</scaleName>
<jsfunc>custTbl1</jsfunc>
</custscale>
Use addChild()
and 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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With