1

Hi every one I have an XML file, it's contain same tags like this:

<data>
<info><detail>first data</detail></info>
<info><detail>second data</detail></info>
<info><detail>third data</detail></info>
....
</data>

I want to add this:

<info><detail>fourth data</detail></info>

to my file.

and thanks for any help.

2 Answers 2

6
$xml = simplexml_load_file('foo.xml');
$xml->addChild('info')->addChild('detail', 'fourth data');
file_put_contents('foo.xml', $xml->asXML());
Sign up to request clarification or add additional context in comments.

1 Comment

@hashim, I've changed the example to load/save to a file. You should be able to adapt it to your needs.
2

Look into the simplexml extension for PHP

https://www.php.net/simplexml

Specifically https://www.php.net/manual/en/simplexmlelement.addchild.php for adding the node once you have created it.

Comments

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.