Creating a new element and inserting it as root in PHP

Description

The following code shows how to creating a new element and inserting it as root.

Example


//from w  w  w  . j  a  v a 2  s  .  co m

<?php
$dom = new DOMDocument('1.0', 'utf-8');
$element = $dom->createElement('test', 'This is the root element!');
// We insert the new element as root (child of the document)
$dom->appendChild($element);
echo $dom->saveXML();
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    XML »




DOM
SimpleXML
SimpleXMLElement
XML Parser