有没有一种方法可以将PHP中的XML文件转换为JSON

时间:2019-12-07 06:17:41

标签: javascript php jquery json xml

我正在尝试将xml getDetailsXML.php文件转换为JSON。我有一个对我的service.php页面的调用,该页面处理了我对getDetailsXML.php的ajax请求。在service.php中,我正在执行从XML到JSON的转换,然后发回要显示的数据。

我的service.php

<?php
     $.ajax({
            url: "getDetailsXML.php?ImageID=" + escape($itemName),
            cache: false,
            dataType: "xml",
            success: function(xml){
                echo json_encode(xml);
        });
echo $itemName[$_REQUEST['ItemName']];;
?>

我要从服务页面获取详细信息的电话。

    function getDetails(itemName) {
        $.getJSON("service.php?ItemName=" + itemName, function(json){
            var descriptionDetails = "<p class='description'>Description: " + this.description + "</p>" + "<p>Price: " + this.price + "</p>";
            $("#detailsPane").append(descriptionDetails);
            var priceDetails = "<p>Price: " + this.price + "</p>";
            $.each(json.resources, function () {
                var url = "<li>" + this.url + "</li>";
                $("#detailsPane").append(url);
            });
        });
    }

我的getDetailsXML.php文件

<?php

$details = array (
        'itemGuitar'    =>      
"<?xml version=\"1.0\"?>
<item id=\"itemGuitar\">
 <description>Pete Townshend once played this guitar while his own axe was in the shop having bits of drumkit removed from it</description>
 <price>5695.99</price>
 <resources>
   <url>http://www.thewho.com/</url>
   <url>http://en.wikipedia.org/wiki/Pete_Townshend</url>
 </resources>
</item>",
        'itemShades'    =>      
"<?xml version=\"1.0\"?>
<item id=\"itemShades\">
 <description>Yoko Ono's sunglasses. While perhaps not valued much by Beatles fans this pair is rumored to have been licked by John Lennon.</description>
 <price>258.99</price>
 <resources>
   <url>http://www.beatles.com/</url>
   <url>http://johnlennon.com/</url>
   <url>http://www.yoko-ono.com/</url>
 </resources>
</item>",
        'itemCowbell'   =>      
"<?xml version=\"1.0\"?>
<item id=\"itemCowbell\">
 <description>Remember the famous \"more cowbell\" skit
    from Saturday Night Live? Well, this is the actual
    cowbell.</description>
 <price>299.99</price>
 <resources>
   <url>http://www.nbc.com/Saturday_Night_Live/</url>
   <url>http://en.wikipedia.org/wiki/More_cowbell</url>
 </resources>
</item>",
        'itemHat'               =>     
"<?xml version=\"1.0\"?>
<item id=\"itemHat\">
 <description>Michael Jackson's hat as worn in the \"Billie Jean\" video. Not really rock memorabilia but it smells better than Slash's tophat.
 </description>
 <price>1699.99</price>
 <resources>
   <url>http://www.michaeljackson.com/</url>
   <url>http://music.yahoo.com/vid-2143030--Billie-Jean</url>
 </resources>
</item>");

header("Content-Type: text/xml");
echo $details[$_REQUEST['ImageID']];

?>    

1 个答案:

答案 0 :(得分:0)

您可以使用simplexml_load_string将xml加载到对象。然后使用json_encode获取json字符串。

$result = array_map(function($v){return simplexml_load_string($v, 'SimpleXMLElement', LIBXML_NOCDATA);},$details);
echo json_encode($result);