SimpleXMLElement is part of the PHP core. It's an extention that allows us to create or manipulate XML.
addChild() to layout my XML, but what if you need to edit or replace an element afterworth? There is no
replaceChild to accomplish this, nor could you add a position to a new child. There are many ways to solve this, but according to me... This is the way.
To start with, lets create some dummy XML:
$xml = newSimpleXMLElement('<request></request>'); $xml->addChild('name', $name); $xml->addChild('age', $age); $xml->addChild('gender', $gender); $result = $xml->asXML();
This would result in the following output:
<?xml version="1.0"?> <request> <name>John Doe</name> <age>50</age> <gender>Female</gender> </request>
Now if I would like to change the gender from
Male, I would edit or replace the value:
$xml->gender = 'Male'; $result = $xml->asXML();
<?xml version="1.0"?> <request> <name>John Doe</name> <age>50</age> <gender>Male</gender> </request>
That's all folks~