<?php
$dom = new domDocument('1.0');
$root = $dom->createElement('root'); // create new element
foreach (array('foo', 'bar', 'baz') as $v) {
$node = $dom->createElement($v); // create new sub-element
$node->appendChild($dom->createTextNode($v)); // add value to element
$root->appendChild($node); // append sub-element to root element
}
$dom->appendChild($root); // add root node to document
$xml = $dom->saveXML(); // output XML
echo nl2br(htmlspecialchars(str_replace('><', ">\n<", $xml)));
?>
Output
<?xml version="1.0"?>
<root>
<foo>foo</foo>
<bar>bar</bar>
<baz>baz</baz>
</root>
<root>
<foo>foo</foo>
<bar>bar</bar>
<baz>baz</baz>
</root>