Quantcast
Channel: How To – Paul K Leasure
Viewing all articles
Browse latest Browse all 39

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

$
0
0

<?php
// How to APPEND an existing XML Document with PHP's DOMDocument()

//Create an instance of DOMDocument() class
$dom = new DOMDocument();
// Load the existing people.xml file
$dom->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 );


?>


Viewing all articles
Browse latest Browse all 39

Trending Articles