【问题标题】:Is there a way to convert an XML file to JSON in PHP [duplicate]有没有办法在 PHP 中将 XML 文件转换为 JSON [重复]
【发布时间】:2023-04-09 14:37:01
【问题描述】:

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

我的服务.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']];

?>    

【问题讨论】:

标签: javascript php jquery json xml


【解决方案1】:

您可以使用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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-23
    • 2012-05-18
    • 2020-09-18
    • 2016-03-12
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 2015-04-12
    相关资源
    最近更新 更多