/**
  * Convert an Array to XML
  * @param string $node_name - name of the root node to be converted
  * @param array $arr - aray to be converterd
  * @return DomDocument
  */
 public static function &createXML($node_name, $arr = array())
 {
     $xml = self::getXMLRoot();
     $xml->appendChild(self::convert($node_name, $arr));
     self::$xml = null;
     // clear the xml node in the class for 2nd time use.
     return $xml;
 }
示例#2
0
 /**
  * Generate Response
  *
  * Generates a response after determining the accepted response from the client
  *
  * @param mixed $body Array of what should be in the body
  * @return string XML or JSON or WHATever
  * @access private
  */
 private function generateResponse($body)
 {
     $ret = false;
     if (!is_array($body)) {
         $body = array("message" => $body);
     }
     $accepts = explode(",", $this->req->headers->accept);
     foreach ($accepts as $accept) {
         //strip off content accept priority
         $accept = preg_replace('/;(.*)/i', '', $accept);
         switch ($accept) {
             case "text/json":
             case "application/json":
                 $this->addHeader('Content-Type', 'text/json');
                 return json_encode($body);
                 break;
             case "text/xml":
             case "application/xml":
                 $this->addHeader('Content-Type', 'text/xml');
                 //DOMDocument provides us with pretty print XML. Which is...pretty.
                 require_once dirname(__FILE__) . '/Array2XML2.class.php';
                 $xml = \Array2XML2::createXML('response', $body);
                 return $xml->saveXML();
         }
     }
     //If nothing is defined then just default to showing json
     //TODO: move this up into the switch statement?
     $this->addHeader('Content-Type', 'text/json');
     return json_encode($body);
 }