PHP世纪论坛

 找回密码
 立即注册
搜索
查看: 772|回复: 2

php的json数据如何转换xml格式

[复制链接]

3

主题

5

帖子

377

积分

超级版主

Rank: 8Rank: 8

积分
377
发表于 2016-3-1 10:06:56 | 显示全部楼层 |阅读模式
1铜币
  1. [{"id":"36675","catid":"199","typeid":"0","title":"2014\u5e74\u5ea6\u4e2d\u56fd\u7eff\u8272\u6c7d\u8f66\u8bc4\u9009 \u5ba2\u8f66\u5165\u56f4\u8f66\u578b\u4e00\u89c8","style":"","thumb":"http:\/\/www.d1ev.com\/d\/file\/contents\/2015\/01\/54a8fdff78da9.jpg","keywords":"GNEV,\u7eff\u8272\u6c7d\u8f66,\u8bc4\u9009,\u5ba2\u8f66","tags":"GNEV,\u7eff\u8f66\u8bc4\u9009","description":"\u4e2d\u56fd\u5e74\u5ea6\u7eff\u8272\u6c7d\u8f66\u8bc4\u9009"\u4f5c\u4e3a\u5168\u7403\u65b0\u80fd\u6e90\u6c7d\u8f66\u5927\u4f1a\uff08GNEV\uff09\u91cd\u8981\u7684\u4e00\u73af\uff0c\u7531\u7b2c\u4e00\u7535\u52a8\u53d1\u8d77\uff0c\u662f\u6df1\u53d7\u5e02\u573a\u548c\u5e7f\u5927\u6d88\u8d39\u8005\u8ba4\u53ef\u7684\u65b0\u80fd\u6e90\u6c7d\u8f66\u8bc4\u9009\u3002\u672c\u6587\u4e3a\u5ba2\u8f66\u5165\u56f4\u8f66\u578b\u4e00\u89c8\u3002","posid":"1","url":"http:\/\/www.d1ev.com\/36675.html","listorder":"8","status":"99","sysadd":"1","islink":"0","username":"yangshiming","inputtime":"1420360256","updatetime":"1420360256","views":"4211","yesterdayviews":"49","dayviews":"11","weekviews":"11","monthviews":"11","viewsupdatetime":"1456797474","m_title":"2014\u5e74\u5ea6\u4e2d\u56fd\u7eff\u8272\u6c7d\u8f66\u8bc4\u9009 \u5ba2\u8f66","copyfrom":"","newstype":"\u4e8c\u7c7b","copyright":"\u539f\u521b","author":"\u6768\u4e16\u660e","shengming":"1","level":"60","sheng":"","sp_id":"","shi":"","pvuser":"\u6768\u4e16\u660e","shuxing":"jishi","zuozhe":"732","thumb2":"http:\/\/www.d1ev.com\/d\/file\/contents\/2015\/01\/54a8fed020c76.jpg","zixun":"0","zixun_status":"0","zixun_day":"0"},{"id":"36660","catid":"93","typeid":"0","title":"\u5e86\u7b2c\u4e00\u7535\u52a8\u7f51\u65b0\u7248\u4e0a\u7ebf \u9a7e\u5b9d\u9a6ci3\u91c7\u8d2d\u5458\u5de5\u798f\u5229","style":"","thumb":"http:\/\/www.d1ev.com\/d\/file\/contents\/2015\/01\/54a8a214989cd.jpg","keywords":"\u5b9d\u9a6ci3","tags":"\u5b9d\u9a6ci3","description":"\u6070\u9022\u5143\u65e6\u4e0e\u7b2c\u4e00\u7535\u52a8\u7f51\u65b0\u7248\u6b63\u5f0f\u4e0a\u7ebf\u53cc\u559c\u4e34\u95e8\uff0c\u91c7\u8d2d\u5458\u5de5\u798f\u5229\u4e0e\u5e86\u795d\u65b0\u7248\u7f51\u9875\u4e0a\u7ebf\u6240\u9700\u7269\u54c1\u7684\u4efb\u52a1\u8feb\u5728\u7709\u776b\u3002\u5b9d\u9a6ci3\u4f5c\u4e3a\u7eaf\u7535\u52a8\u8f66\u4ee3\u8868\u4e4b\u4e00\uff0c\u81ea\u7136\u4e0d\u8d1f\u4f17\u671b\u3002","posid":"1","url":"http:\/\/www.d1ev.com\/36660.html","listorder":"7","status":"99","sysadd":"1","islink":"1","username":"yangbo","inputtime":"1420337711","updatetime":"1420337711","views":"371","yesterdayviews":"2","dayviews":"1","weekviews":"1","monthviews":"1","viewsupdatetime":"1456775790","m_title":"\u9a7e\u5b9d\u9a6ci3\u91c7\u8d2d\u798f\u5229","copyfrom":"","newstype":"\u4e00\u7c7b","copyright":"\u539f\u521b","author":"\u738b\u4e00","shengming":"1","level":"60","sheng":"\u5b9d\u9a6c","sp_id":"","shi":"\u5b9d\u9a6ci3","pvuser":"\u738b\u4e00","shuxing":"xuanche","zuozhe":"1513","thumb2":"http:\/\/www.d1ev.com\/d\/file\/contents\/2015\/01\/54a8a21cc559d.jpg","zixun":"0","zixun_status":"0","zixun_day":"0"}]
复制代码
这些json数据转换成xml格式,如
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <rss version="2.0">
  3. <channel>
  4. <title>媒体名称/定义网站频道名称</title>
  5. <description>媒体名称/定义网站频道介绍</description>
  6. <link>http://www.toutiao.com</link> //网站频道地址
  7. <generator>生成RSS所用的程序</generator>
  8. <image>
  9. <url>LOGO图片地址(绝对地址)</url>
  10. <title>图片替代文字</title>
  11. <link>http://www.toutiao.com</link> // 网站首页地址
  12. </image>
  13. <item>
  14. <title><![CDATA[ 文章标题 ]]></title>
  15. <link>文章URL地址(绝对地址)</link>
  16. <description><![CDATA[ 摘要/全文 ]]></description>
  17. <source>来源名称</source>
  18. <pubDate>Mon, 07 Jul 2014 13:42:28 +0800</pubDate> // 最后发布时间
  19. </item>
  20. </channel>
  21. </rss>
复制代码

回复

使用道具 举报

234

主题

240

帖子

1074

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
1074
发表于 2016-3-1 10:37:23 | 显示全部楼层
  1. <?php

  2. /**
  3. * 这是一种简单的做法,同时这种只能生成单个频道的信息
  4. * 注:
  5. *     1.来源没有定义
  6. *     2.时间戳没有格式化
  7. */

  8. $json='json';
  9. //将json转为对象
  10. $rows=json_decode($json);
  11. //var_dump($rows);

  12. $xml='
  13. <?xml version="1.0" encoding="utf-8"?>
  14. <rss version="2.0">
  15. <channel>
  16. <title>媒体名称/定义网站频道名称</title>
  17. <description>媒体名称/定义网站频道介绍</description>
  18. <link>http://www.toutiao.com</link>
  19. <generator>生成RSS所用的程序</generator>
  20. <image>
  21. <url>LOGO图片地址(绝对地址)</url>
  22. <title>图片替代文字</title>
  23. <link>http://www.toutiao.com</link>
  24. </image>';
  25. foreach($rows as $row){
  26. $xml.='
  27. <item>
  28. <title><![CDATA[ '.$row->title.' ]]></title>
  29. <link>'.$row->url.'</link>
  30. <description><![CDATA[ '.$row->description.' ]]></description>
  31. <source>来源名称</source>
  32. <pubDate>'.$row->inputtime.'</pubDate>
  33. </item>
  34. ';
  35. }
  36. $xml.='
  37. </channel>
  38. </rss>
  39. ';
  40. echo $xml;
复制代码
回复

使用道具 举报

3

主题

5

帖子

377

积分

超级版主

Rank: 8Rank: 8

积分
377
 楼主| 发表于 2016-3-1 13:48:09 | 显示全部楼层
xml页面显示不出标签,代码加header("Content-type:text/xml");即可
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|PHP世纪论坛 ( 豫ICP备15004666号-2 )

GMT+8, 2018-5-24 21:59

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表