Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/228.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有没有办法通过PHP将XML页面自动转换为JSON?_Php_Javascript_Xml_Json - Fatal编程技术网

有没有办法通过PHP将XML页面自动转换为JSON?

有没有办法通过PHP将XML页面自动转换为JSON?,php,javascript,xml,json,Php,Javascript,Xml,Json,可能重复: 以下是我的输出XML示例: <item> <Barcode>0602527522593</Barcode> <albumid>1818</albumid> <Title>Gold</Title> <Country>Sweden</Country> <Format>CD</Format> <Length></Length&g

可能重复:

以下是我的输出XML示例:

<item>
 <Barcode>0602527522593</Barcode>
 <albumid>1818</albumid>
 <Title>Gold</Title>
 <Country>Sweden</Country>
 <Format>CD</Format>
 <Length></Length>
 <Number_of_Discs></Number_of_Discs>
 <Type>album</Type>
 <Description></Description>
 <Band_or_Artist>Adams, Ryan</Band_or_Artist>
</item>

0602527522593
1818
黄金
瑞典
光盘
专辑
亚当斯,瑞安

是否有易于使用的内置PHP函数或外接程序可以快速将其转换为JSON并输出?如果不是内置的,我应该使用哪个扩展?

作为在客户端执行此操作的替代方法,是来自fyneworks的扩展。

有很多。这里有一个来自IBM的示例:

您可以使用SimpleXML(http://php.net/manual/en/book.simplexml.php)读取XML输入,然后进行json_编码(http://php.net/manual/en/book.json.php)创建输出

您可能只想迭代节点列表,并使用标记名作为键将所有内容放在一个数组中,这应该相当简单。

类似的内容

<?php

$xmlstr = "<item>
 <Barcode>0602527522593</Barcode>
 <albumid>1818</albumid>
 <Title>Gold</Title>
 <Country>Sweden</Country>
 <Format>CD</Format>
 <Length></Length>
 <Number_of_Discs></Number_of_Discs>
 <Type>album</Type>
 <Description></Description>
 <Band_or_Artist>Adams, Ryan</Band_or_Artist>
</item>";

$movies = new SimpleXMLElement($xmlstr);
echo $output = json_encode($movies);
?>

既然您说这个XML是您的输出,那么从源数据生成JSON可能比生成XML然后将其转换为JSON更容易。数据来自哪里?