lundi 23 mai 2016

Trying to return XML with laravel. wrong parse xml

Hi im trying to make a xml file with laravel 5.2.

My function What i commented out i tried, wrong xml parse.

    public function createXML() {
        $maps = Maps::get()->toArray();

//        function array_to_xml(array $arr, SimpleXMLElement $xml)
//        {
//            foreach ($arr as $k => $v) {
//                is_array($v)
//                    ? array_to_xml($v, $xml->addChild($k))
//                    : $xml->addChild($k, $v);
//            }
//            return $xml;
//        }
        function to_xml(SimpleXMLElement $object, array $data)
        {
            foreach ($data as $key => $value) {
                if (is_array($value)) {
                    $new_object = $object->addChild($key);
                    to_xml($new_object, $value);
                } else {
                    $object->addChild($key, $value);
                }
            }
        }

//        $xmloutput = to_xml($maps, new SimpleXMLElement('<root/>'))->asXML();
        $xml = new SimpleXMLElement('<rootTag/>');
        to_xml($xml, $maps);

        return Response::make($xml->asXML())->header('Content-Type', 'text/xml');

enter image description here

my output array:

array:2 [▼
  0 => array:5 [▼
    "id" => 1
    "name" => "Leeuwarden"
    "address" => "Leeuwarden"
    "lat" => 53.20132
    "lng" => 5.80005
  ]
  1 => array:5 [▼
    "id" => 2
    "name" => "Assen"
    "address" => "Assen"
    "lat" => 52.99275
    "lng" => 6.56423
  ]
]

Any suggestions on what i am doing wrong?

i'm trying to make a google maps API map with pointers from the database.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/1s4vRSx
via IFTTT

Aucun commentaire:

Enregistrer un commentaire