To create an element with DomDocument and to escape ampersand in the value.
Do this:
$element = new DOMDocument('1.0', 'UTF-8');
$test = $element->createElement('text');
$test ->appendChild($element->createElement('name'))
->appendChild($element->createtextNode('& I am ampersand');