Its all about the DOMDocument and generating the XML from the same with examples. I have created some of the good documents with the examples for generating the xml and formatting the same. You will get most of the functions with the examples are written in the following post :
$dom = new DOMDocument('1.0', 'UTF-8');
$root = $dom->createElement('root');
$dom->appendChild($root);
echo $dom->saveXML();
1. Creating XML Document and Displaying :