项目 >> REST >> JSON

将xml转换成json最简单的办法

在php5下,将xml转换成json最简单的办法,就是利用simplexml和json扩展。

废话不多说,贴代码:

<?php
$xml = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book id="1">
		<title>Code Generation in Action</title>
		<author><first>Jack</first><last>Herrington</last></author>
		<publisher>Manning</publisher>
	</book>
	<book id="2">
		<title>PHP Hacks</title>
		<author><first>Jack</first><last>Herrington</last></author>
		<publisher>O'Reilly</publisher>
	</book>
	<book id="3">
		<title>Podcasting Hacks</title>
		<author><first>Jack</first><last>Herrington</last></author>
		<publisher>O'Reilly</publisher>
	</book>
</books>
EOF;
echo $json = json_encode(simplexml_load_string($xml));
?>

 

整个过程就是一行,灰墙之ez.

另:可以跟以前写的“用php将rss转化为json格式”比较一下.(来源 www.iocblog.net)




文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。