Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
DOMDocument code generating malformed XML.
$plantList = $this->em->getRepository('Entities\Plant')->toGoogleProductsFormat();
$xml = new DOMDocument('1.0', 'utf-8');
$rss = $xml->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttribute('xmlns:g', '');
$rss->setAttribute('xmlns:c', '');
//Create channel element
$channel = $xml->createElement('channel');
$channel = $rss->appendChild($channel);
//Loop through each plant
foreach($plantList as $plantItem){
//begin item element
$item = $xml->createElement('item');
//Loop through use key as element name and value as data
foreach ($plantItem as $key => $value)
//Decode HTML characters, for example '&' becomes &
//to comply with
$decode = htmlspecialchars_decode($value);
$decode = trim($decode);
//Create the element
$tag = $xml->createElement($key);
$tag = $item->appendChild($tag);
//Write the field
$text = $xml->createTextNode($decode);
$text = $tag->appendChild($text);
$item = $channel->appendChild($item);
//Output xml to body
$output = $xml->SaveXML();
$validator = new XMLValidator($xml);
$this->_response->setHeader('Content-Type', 'text/xml; charset=utf-8')->setBody($output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment