PHP: How to Append an XML File with PHP’s DOMDocument Object

load('people.xml');

//create member node
$personNode = $dom->createElement('person');
    //Add attribute to the person node
		$personNode->setAttribute('id', 5 );

//create and add 1st Name to member Node
$firstNameNode = $dom->createElement('firstName');
	  //create a text node
	  $firstNameTextNode = $dom->createTextNode('Sue');
		// Append the text node as a child to the firstname node
		$firstNameNode->appendChild( $firstNameTextNode );

//create and add last Name to member Node
$lastNameNode = $dom->createElement('lastName');
	  //create a text node
		$lastNameTextNode = $dom->createTextNode('Moore');
		// Append the text node as a child to the firstname node
		$lastNameNode->appendChild( $lastNameTextNode );


?>

Leave a Reply

Your email address will not be published. Required fields are marked *