尝试使用laravel返回XML。(Trying to return XML with laravel. wrong parse xml)

编程入门 行业动态 更新时间:2024-10-28 04:28:24
尝试使用laravel返回XML。(Trying to return XML with laravel. wrong parse xml)

嗨我试图用laravel 5.2制作一个xml文件。

我的功能我试过注释我试过,错误的xml解析。

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');

我的输出数组:

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 ] ]

关于我做错了什么的任何建议?

我正在尝试使用数据库中的指针制作谷歌地图API地图。

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');

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.

最满意答案

第一个选项:直接在地图中获取正确的结构并命名数组。

第二个选项:如果你不能更改收到的$maps数组,那么你可以通过添加检查来修改头部级别:

$maps = [ 0 => [ "id" => 1, "name" => "Leeuwarden", "address" => "Leeuwarden", "lat" => 53.20132, "lng" => 5.80005, ], 1 => [ "id" => 2, "name" => "Assen", "address" => "Assen", "lat" => 52.99275, "lng" => 6.56423, ] ]; function to_xml(\SimpleXMLElement $object, array $data, $level = 0) { foreach ($data as $key => $value) { if (is_array($value)) { $new_object = $object->addChild(($level == 0) ? 'marker' : $key); to_xml($new_object, $value, $level + 1); } else { $object->addChild($key, $value); } } } $xml = new \SimpleXMLElement('<rootTag/>'); to_xml($xml, $maps); header('Content-type: text/xml'); echo $xml->asXML();

编辑:我检查了文档 ,似乎第一级需要命名为marker 。

1st option: get the right structure directly in maps and name the arrays.

2nd option: if you can'y change the $maps array received, then you can modify the head level by adding check to it:

$maps = [ 0 => [ "id" => 1, "name" => "Leeuwarden", "address" => "Leeuwarden", "lat" => 53.20132, "lng" => 5.80005, ], 1 => [ "id" => 2, "name" => "Assen", "address" => "Assen", "lat" => 52.99275, "lng" => 6.56423, ] ]; function to_xml(\SimpleXMLElement $object, array $data, $level = 0) { foreach ($data as $key => $value) { if (is_array($value)) { $new_object = $object->addChild(($level == 0) ? 'marker' : $key); to_xml($new_object, $value, $level + 1); } else { $object->addChild($key, $value); } } } $xml = new \SimpleXMLElement('<rootTag/>'); to_xml($xml, $maps); header('Content-type: text/xml'); echo $xml->asXML();

Edit: I checked the docs, it seems, that the 1st level needs to be named marker.

更多推荐

本文发布于:2023-08-06 07:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1445011.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:return   XML   laravel   xml   parse

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!