Example #1
0
 /**
  * Convert object attributes to XML
  *
  * @param  array $arrAttributes array of required attributes
  * @param string $rootName name of the root element
  * @return string
  */
 protected function __toXml(array $arrAttributes = array(), $rootName = 'item', $addOpenTag = false, $addCdata = true)
 {
     $xml = '';
     if ($addOpenTag) {
         $xml .= '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
     }
     if (!empty($rootName)) {
         $xml .= '<' . $rootName . '>' . "\n";
     }
     $xmlModel = new SimpleXMLElement('<node></node>');
     $arrData = $this->toArray($arrAttributes);
     foreach ($arrData as $fieldName => $fieldValue) {
         if ($addCdata === true) {
             $fieldValue = "<![CDATA[{$fieldValue}]]>";
         } else {
             $fieldValue = $xmlModel->xmlentities($fieldValue);
         }
         $xml .= "<{$fieldName}>{$fieldValue}</{$fieldName}>" . "\n";
     }
     if (!empty($rootName)) {
         $xml .= '</' . $rootName . '>' . "\n";
     }
     return $xml;
 }