public

DOMDocument code generating malformed XML.

  • Download Gist
malformed_domdocument.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
<?php
$plantList = $this->em->getRepository('Entities\Plant')->toGoogleProductsFormat();
 
$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput=true;
$rss = $xml->createElement('rss');
$rss->setAttribute('version', '2.0');
$rss->setAttribute('xmlns:g', 'http://base.google.com/ns/1.0');
$rss->setAttribute('xmlns:c', 'http://base.google.com/cns/1.0');
$xml->appendChild($rss);
//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 http://www.w3.org/TR/xhtml1/#C_12
$decode = htmlspecialchars_decode($value);
$decode = trim($decode);
if(empty($decode))
continue;
//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();
//Validate
$validator = new XMLValidator($xml);
$this->_response->setHeader('Content-Type', 'text/xml; charset=utf-8')->setBody($output);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.