5

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>
1
  • Into which concrete problem are you running? Where is your code where you run into the error? What specifically does not work? Where do you hit the rock? Any error messages? Which of the existing solutions here on this website did not work for you? Commented Apr 30, 2013 at 2:53

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

@sonnet: edit your question and include your PHP! It's easier to point you in the right direction then!
I've created a new php with your code only changed the line $xml = simplexml_load_file('my_xml_file.xml'); but it is not workng.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.