The Slim Framework support forum has moved to http://discourse.slimframework.com. This Tender forum is no longer maintained or monitored.

Creating XML Output

Christian B.'s Avatar

Christian B.

02 Aug, 2012 09:02 AM

Hi there!

This is my Route:

$app -> get('/taskmanager/export/organizrrr.xml', $AuthCheck, function() use ($app) {
    $TASKS = Model::factory('organizrrr_taskmanager') -> order_by_asc('status') -> find_many();
    $COUNTR = Model::factory('organizrrr_taskmanager') -> count();
    return $app -> render('taskmanager_xml_export.xml', array(
        'BASE_URL'   => _BASE_URL,
        'TASKS'      => $TASKS
    ));
});

This is my Template:

<?php

@header("Content-Type: text/xml; charset: utf-8");
@header("Pragma: no-cache");
@header("Expires: 0");
$NL      = chr(13) . chr(10);

echo '<' . chr(63) . 'xml version="1.0" encoding="UTF-8" ' . chr(63) . '>' . $NL;
echo '' . $NL;

echo '<taskmanager>' . $NL;

    if($TASKS){
        foreach($TASKS as $TASK){
            echo '<task>' . $NL;
            echo '<task_id>' . $TASK->id . '</task_id>' . $NL;
            echo '<timestamp>' . $TASK->timestamp . '</timestamp>' . $NL;
            echo '<title>' . $TASK->title . '</title>';
            echo '<description>' . $TASK->description . '</description>';
            echo '</task>' . $NL;
        }
    }

echo '</taskmanager>' . $NL;

echo '';

This is my Output:




13
2012-07-26 15:32:29
Lorem 2Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

12
2012-07-26 14:56:16
Lorem Ipsum dolor sit ametLorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

I need to output some real XML...but i do not know, how to reach this???

  1. Support Staff 1 Posted by Brian Nesbitt on 02 Aug, 2012 05:29 PM

    Brian Nesbitt's Avatar

    You have a few options here. If you want to just use the default Slim view and put the rendering code into the view then you can see below for a working example. The other option is to create a custom XmlView that would expose an xml encode function for you. Also you could override the render() method and have the template parameter be the object to encode. All depends on how fancy / auto magical you want to get.

    //http://stackoverflow.com/questions/137021/php-object-as-xml-document
    class XmlSerializer {
        public static function encode($obj, $node_block='nodes', $node_name='node') {
            if ($obj instanceof stdClass) {
                return self::generateValidXmlFromObj($obj, $node_block, $node_name);
            }
            elseif (is_array($obj)) {
                return self::generateValidXmlFromArray($obj, $node_block, $node_name);
            }
        }
    
        public static function generateValidXmlFromObj(stdClass $obj, $node_block='nodes', $node_name='node') {
            $arr = get_object_vars($obj);
            return self::generateValidXmlFromArray($arr, $node_block, $node_name);
        }
    
        public static function generateValidXmlFromArray($array, $node_block='nodes', $node_name='node') {
            $xml = '<?xml version="1.0" encoding="UTF-8" ?>';
    
            $xml .= '<' . $node_block . '>';
            $xml .= self::generateXmlFromArray($array, $node_name);
            $xml .= '</' . $node_block . '>';
    
            return $xml;
        }
    
        private static function generateXmlFromArray($array, $node_name) {
            $xml = '';
    
            if (is_array($array) || is_object($array)) {
                foreach ($array as $key=>$value) {
                    if (is_numeric($key)) {
                        $key = $node_name;
                    }
    
                    $xml .= '<' . $key . '>' . self::generateXmlFromArray($value, $node_name) . '</' . $key . '>';
                }
            } else {
                $xml = htmlspecialchars($array, ENT_QUOTES);
            }
    
            return $xml;
        }
    }
    
    define('_BASE_URL', "http://127.0.0.1/");
    $AuthCheck = function() {};
    
    $app->get('/taskmanager/export/organizrrr', $AuthCheck, function() use ($app) {
        $app->contentType("text/xml");
        $TASKS = array(array('task_id' => 1, 'timestamp' => 'time1', 'title' => 'title1', 'description' => 'description1'), array('task_id' => 2, 'timestamp' => 'time2', 'title' => 'title2', 'description' => 'description2')); //Model::factory('organizrrr_taskmanager')->order_by_asc('status') -> find_many();
        $COUNTR = count($TASKS); //Model::factory('organizrrr_taskmanager')->count();
        return $app->render('taskmanager_xml_export.xml', array('BASE_URL' => _BASE_URL, 'TASKS' => $TASKS));
    });
    

    The view is then just:

    <? echo XmlSerializer::encode($TASKS, 'tasks', 'task'); ?>
    

    I grabbed the php object/array to Xml code from stackoverflow so you can use it or find something else.
    I also updated the $COUNTR variable to be a count() of the returned tasks which would prevent another db hit but may or may not be correct.

  2. 2 Posted by Christian B. on 03 Aug, 2012 06:19 AM

    Christian B.'s Avatar

    OK - i get XML ;-) Thanxs but:

    • I included the XML Class in main index.php
    • i defined my view:

    $app -> get('/taskmanager/export/xml/', $AuthCheck, function() use ($app) {

        $app->contentType("text/xml");
        $TASKS = Model::factory('organizrrr_taskmanager')->order_by_asc('status') -> find_many();
        $COUNTR = count($TASKS); 
        return $app -> render('taskmanager_xml.xml', array(
            'TASKS'      => $TASKS
        ));
    });
    
    • the result:

    <?xml version="1.0" encoding="UTF-8" ?>

    • it is empty???? it returns an empty orm - Element???
  3. Support Staff 3 Posted by Brian Nesbitt on 03 Aug, 2012 07:22 PM

    Brian Nesbitt's Avatar

    The XML serializer uses get_object_vars() http://php.net/manual/en/function.get-object-vars.php to then pass into the array serializer. Depending on how your particular ORM provides access to the object properties (maybe uses get/set instead) it may not simply just work as the properties aren't simple public properties of the object.

    You can look for a get object properties method that will return an array which you can then pass into the encode() function. The other (last case) option is to write a simple mapper.

    It does look like you are using Paris in which case they do use get/set which means passing the model object won't work. But as mentioned you can use their as_array() method to obtain the properties as an array and simply pass that.

  4. 4 Posted by Christian B. on 05 Aug, 2012 03:27 AM

    Christian B.'s Avatar

    OK ;-)

    $app->contentType("text/xml");

    Thats all i need - working fine for me! Thanx!

  5. Support Staff 5 Posted by Brian Nesbitt on 05 Aug, 2012 03:28 AM

    Brian Nesbitt's Avatar

    Great!

  6. Brian Nesbitt closed this discussion on 05 Aug, 2012 03:28 AM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac