Speed up development with full-stack environments for every branch.

Learn More

How to create a simple XML document with DOMDocument class [PHP]

765 Runs 3795 Views 1650 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/**
 * PHP DOM module
 * Creating a simple xml document with DOMDocument class
 * 
 */

//create a new document
$domDocument = new DOMDocument('1.0', "UTF-8");
//create few elements
$domElement = $domDocument->createElement('birds','i love birds');
$domElement1 = $domDocument->createElement('pigs','i eat pigs');
$domElement2 = $domDocument->createElement('cats','i have cats');
$domElement3 = $domDocument->createElement('fishes','i eat fishes');


// Append the elements to the document itself
$domDocument->appendChild($domElement);
$domDocument->appendChild($domElement1);
$domDocument->appendChild($domElement2);
$domDocument->appendChild($domElement3);

//to see the full output of the document 
echo '<center><pre>'.htmlentities($domDocument->saveXML(), ENT_COMPAT | ENT_HTML401, "ISO-8859-1").'</pre></center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).