<?php
$doc = new DOMDocument("1.0","UTF-8");
$doc->appendChild($root = $doc->createElement("root"));
$root->appendChild($doc->createElement("sub1"));
$root->appendChild($node = $doc->createElement("sub2"));
$node->appendChild($doc->createCDATASection("cdata text"));
$root->appendChild($node = $doc->createElement("sub3"));
$node->appendChild($doc->createTextNode("just plain old text"));
$root->appendChild($doc->createElement("sub5"));
$doc->formatOutput = true;
echo $doc->saveXML();