It throws exception if the tag name or attribute name has illegal chars. Author : Lalit Patel Website: http://www.lalit.org/lab/convert-php-array-to-xml-with-attributes License: Apache License 2.0 http://www.apache.org/licenses/LICENSE-2.0 Version: 0.1 (10 July 2011) Version: 0.2 (16 August 2011) - replaced htmlentities() with htmlspecialchars() (Thanks to Liel Dulev) - fixed a edge case where root node has a false/null/0 value. (Thanks to Liel Dulev) Version: 0.3 (22 August 2011) - fixed tag sanitize regex which didn't allow tagnames with single character. Version: 0.4 (18 September 2011) - Added support for CDATA section using @cdata instead of @value. Version: 0.5 (07 December 2011) - Changed logic to check numeric array indices not starting from 0. Version: 0.6 (04 March 2012) - Code now doesn't @cdata to be placed in an empty array Version: 0.7 (24 March 2012) - Reverted to version 0.5 Version: 0.8 (02 May 2012) - Removed htmlspecialchars() before adding to text node or attributes. Usage: $xml = Array2XML::createXML('root_node_name', $php_array); echo $xml->saveXML();
Exemple #1
0
 protected function encode($cmd, $root = 'request')
 {
     switch ($this->encoding) {
         default:
         case self::ENC_RAW:
             //void
             break;
         case self::ENC_SERIALIZE:
             $cmd = serialize($cmd);
             break;
         case self::ENC_XML:
             try {
                 $cmd = Array2XML::createXML($root, $cmd)->saveXML();
             } catch (Exception $e) {
                 throw new Exception('Could not encode XML: ' . print_r($cmd));
             }
             break;
         case self::ENC_JSON:
             $cmd = json_encode($cmd);
             break;
     }
     //add encoding type
     $cmd = chr($this->encoding) . $cmd;
     return $cmd;
 }
 /**
  * Convert an Array to XML
  *
  * @param        $file_name
  * @param string $root_node_name - name of the root node to be converted
  * @param array  $data           - array to be converted
  *
  * @internal param $child_node_name
  * @return string
  */
 public function toXML($file_name, $root_node_name, array $data)
 {
     $xmlObj = Array2XML::createXML($root_node_name, $data);
     $xml = $xmlObj->saveXML();
     file_put_contents($file_name, $xml);
     return $xml;
 }
Exemple #3
0
 public function display()
 {
     $this->requireSettings();
     $displaydata = $this->prepareResponseStructure();
     header('Content-type: application/xml');
     $xml = \LSS\Array2XML::createXML('document', $displaydata);
     echo $xml->saveXML();
     return true;
 }
 /**
  * @param mixed $data
  * @return Response
  */
 public function sendData($data, $root = '')
 {
     if (!$root) {
         throw new \Exception('xml root empty');
     }
     $xmlDom = Array2XML::createXML($root, $this->getData());
     $headers = array('Content-Type' => 'text/xml; charset=utf-8');
     $httpResponse = $this->httpClient->post($this->getEndpoint(), $headers, $xmlDom->saveXML())->send();
     return $this->response = new Response($this, $httpResponse->getBody(true));
 }
Exemple #5
0
 public function toFormat($data)
 {
     try {
         if (!is_int(key($data))) {
             Array2XML::init('1.0', 'UTF-8', false);
             $xml = Array2XML::createXML($this->options['root'], $data);
             return $xml->saveXML();
         } else {
             Array2XML::init('1.0', 'UTF-8', false);
             $xml = Array2XML::createXML($this->options['list'], array($this->options['root'] => $data));
             return $xml->saveXML();
         }
     } catch (\Exception $e) {
         $di = \Phalcon\DI\FactoryDefault::getDefault();
         $di['response']->internalServerError('Unable to write format.');
     }
 }
 public function render($response, array $data)
 {
     $contentType = $this->determineContentType($this->request->getHeaderLine('Accept'));
     switch ($contentType) {
         case 'text/html':
             $output = $this->renderHtml($data);
             break;
         case 'application/xml':
             $xml = Array2XML::createXML('root', $data);
             $output = $xml->saveXML();
             break;
         case 'application/json':
         default:
             $contentType = 'application/json';
             $output = json_encode($data);
             break;
     }
     $body = new Body(fopen('php://temp', 'r+'));
     $body->write($output);
     return $response->withHeader('Content-type', $contentType)->withBody($body);
 }
 /**
  * XML Conversion
  *
  * @param array $data
  *
  * @return string
  */
 public function convert($data = [])
 {
     $xml = Array2XML::createXML('xmlrequest', $data);
     return $xml->saveXML();
 }
 public static function createXmlFromArray(array $array, $rootTagName)
 {
     return Array2XML::createXML($rootTagName, $array);
 }
 public function SiteMapXMLAction(Request $request)
 {
     $logger = $this->get('logger');
     $result = array();
     $em = $this->getDoctrine()->getManager();
     $routes = $em->getRepository('NovuscomCMFBundle:Route')->findBy(array('active' => true));
     $Route = $this->get('Route');
     $Site = $this->get('Site');
     $currentAlias = $Site->getAlias();
     $alias = $currentAlias['name'];
     $prefix = 'http://' . $alias;
     $needRoutes = array('@attributes' => array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'), 'url' => array());
     $urlArray = array();
     /*
      * Страницы
      */
     $Site = $this->get('Site');
     $currentSite = $Site->getCurrentSite();
     $siteRef = $em->getReference('Novuscom\\CMFBundle\\Entity\\Site', $currentSite['id']);
     $pagesRepo = $em->getRepository('NovuscomCMFBundle:Page');
     $pages = $pagesRepo->findBy(array('site' => $siteRef));
     foreach ($pages as $p) {
         $codes = array();
         foreach ($pagesRepo->getPath($p) as $path) {
             $codes[] = $path->getUrl();
         }
         $url = implode('/', $codes);
         $url = str_replace('//', '/', $url);
         if ($p->getLvl() > 0) {
             $url = trim($url, '/');
             $url = $this->get('router')->generate('page', array('url' => $url));
         } else {
             $url = $this->get('router')->generate('cmf_page_main');
         }
         $url = $prefix . $url;
         $urlArray[] = $url;
     }
     foreach ($routes as $r) {
         $params = json_decode($r->getParams(), true);
         if ($r->getBlock() === false) {
             continue;
         }
         if ($r->getController() == 'NovuscomCMFBundle:Component:Element') {
             foreach ($r->getBlock()->getElement() as $element) {
                 if ($element->getActive() === false) {
                     continue;
                 }
                 $url = $Route->getUrl($r->getCode(), $element);
                 if ($url) {
                     $url = $prefix . $url;
                     $urlArray[] = $url;
                 }
             }
         }
         if ($r->getController() == 'NovuscomCMFBundle:Component:Section') {
             foreach ($r->getBlock()->getSection() as $section) {
                 $url = $Route->getUrl($r->getCode(), $section);
                 if ($url !== false) {
                     $url = $prefix . $url;
                     $urlArray[] = $url;
                 }
             }
         }
     }
     $urlArray = array_unique($urlArray);
     sort($urlArray);
     foreach ($urlArray as $u) {
         $needRoutes['url'][]['loc'][] = $u;
     }
     $xml = Array2XML::createXML('urlset', $needRoutes)->saveXML();
     $response = new Response();
     $response->headers->set('Content-Type', 'application/xml; charset=UTF-8');
     $response->setContent($xml);
     return $response;
 }
 /**
  * 创建payload字符串
  */
 protected function makePayload()
 {
     $this->payload['@attributes'] = ['xmlns' => 'http://mqs.aliyuncs.com/doc/v1/'];
     $class = class_basename(get_called_class());
     return Array2XML::createXML(strpos($class, 'Queue') !== false ? 'Queue' : 'Message', $this->payload)->saveXML();
 }
 /**
  * Fixes deployment.xml file
  * @param  string $content
  * @return string
  */
 public function fixXml($content)
 {
     $doc = new \DOMDocument();
     ErrorHandler::start();
     $doc->loadXML($content);
     ErrorHandler::stop(true);
     $data = \LSS\XML2Array::createArray($doc);
     $root = $doc->documentElement;
     $rootName = $root->tagName;
     $data[$rootName]['@attributes']['xmlns'] = $root->getAttribute('xmlns');
     // fix the order of the elements
     $data[$rootName] = self::fixMetaKeyOrder($data[$rootName]);
     $xml = \LSS\Array2XML::createXML($rootName, $data[$rootName]);
     return $xml->saveXML();
 }
Exemple #12
0
 public function encode($object, EncoderOptions $options = null)
 {
     $arr = parent::encode($object, $options);
     return Array2XML::createXML(self::ROOT_NODE_NAME, $arr['processed']);
 }
 /**
  * Prepare xml for request
  *
  * @param string $method Method name
  * @param array $args Method arguments
  *
  * @return string XML fore request
  */
 private function prepareXmlForRequest($method, array $args)
 {
     $arrayToXml = Array2XML::createXML('request', $this->prepareArrayForConvertToXml($method, $args));
     return $arrayToXml->saveXML();
 }
Exemple #14
0
 /**
  * @return string
  */
 public function __toString()
 {
     $xml = Array2XML::createXML('xml', $this->data);
     return substr($xml->saveXML(), 39);
 }
Exemple #15
0
 public function generateXml()
 {
     $xml = Array2XML::createXML('QuestionForm', $this->data);
     $xmlText = $xml->saveXML();
     return preg_replace('/FakeKey_(\\w+)_\\w{6}/', '$1', $xmlText);
 }