PHP SimpleXMLElement addChild method is used to add a new child element to an XML node. This method takes two parameters, the name of the new child element and its value. The new child element is added as the last child of the parent node.
Example 1: Adding a new child element to an XML node using PHP SimpleXMLElement addChild method.
$xml = new SimpleXMLElement(''); $xml->addChild('name', 'John'); $xml->addChild('age', '30'); echo $xml->asXML();
Output:
John 30
Example 2: Adding multiple child elements to an XML node using PHP SimpleXMLElement addChild method.
$xml = new SimpleXMLElement(''); $xml->addChild('name', 'John')->addChild('address', '123 Main St'); $xml->addChild('age', '30'); echo $xml->asXML();
Output:
John 123 Main St
30
This package library is included in the PHP core.
PHP SimpleXMLElement::addChild - 30 examples found. These are the top rated real world PHP examples of SimpleXMLElement::addChild extracted from open source projects. You can rate examples to help us improve the quality of examples.