【问题标题】:How do I convert the following XML data into an array (JSON or otherwise)如何将以下 XML 数据转换为数组(JSON 或其他)
【发布时间】:2014-03-19 11:52:07
【问题描述】:

虽然有很多选项可以将“普通”XML 转换为数组,但我非常想找到一种方法将这些数据转换为我可以用 PHP 处理的数组(它目前设计为由 JQuery 处理)

<?xml version="1.0" encoding="ISO-8859-15"?><root><data><![CDATA[ [{title_id: "284270",
          track_id: "1548617",
          artist: [[20670, 1, "Matthias Vogt", "matthias-vogt"]],
          title: "The Wobble Track",
          title_url: "/title/284270/the-wobble-track",
          track_url: "/track/1548617/the-wobble-track",
          label: [88, "Large Music", "large-music"],
          genre: "Deep House",
          genre_url: "/genre/13/deep-house",
          catnumber: "LAR181",
          promo: false,
          duration: "5:54",
          r_date: "2014-02-17",
          price: {hbr: 1.99, wav: 2.74},
          bought: false,
          image: "http://static.traxsource.com/files/images/271306_large.jpg",
          thumb: "http://static.traxsource.com/scripts/image.php/44x44/271306.jpg",
          mp3: "http://preview.traxsource.com/files/previews/88/1324290-p.mp3",
          waveform: "http://static.traxsource.com/files/wf/1324290-wf.png",
          bpm: "120",
          keysig: "Bmin"}
] ]]></data></root>

这个 xml 字符串中还有大约 99 个对象,所以为了简单起见,我只包含了 1 个

我想将一个数组转换为 JSON 或 PHP 数组 - 谢谢 :)

【问题讨论】:

    标签: php xml arrays json cdata


    【解决方案1】:

    不要。只需在 DOM 上使用 Xpath 来获取部件,在您的情况下是 CDATA 部分中的 JSON 结构。

    结构不是真正的 JSON,而是 Javascript,缺少属性名称的引号。这是修复它的 PHP 手册的a user comment 中的一个很好的正则表达式。

    $xml = <<<'XML'
    <?xml version="1.0" encoding="ISO-8859-15"?><root><data><![CDATA[ [{title_id: "284270",
              track_id: "1548617",
              artist: [[20670, 1, "Matthias Vogt", "matthias-vogt"]],
              title: "The Wobble Track",
              title_url: "/title/284270/the-wobble-track",
              track_url: "/track/1548617/the-wobble-track",
              label: [88, "Large Music", "large-music"],
              genre: "Deep House",
              genre_url: "/genre/13/deep-house",
              catnumber: "LAR181",
              promo: false,
              duration: "5:54",
              r_date: "2014-02-17",
              price: {hbr: 1.99, wav: 2.74},
              bought: false,
              image: "http://static.traxsource.com/files/images/271306_large.jpg",
              thumb: "http://static.traxsource.com/scripts/image.php/44x44/271306.jpg",
              mp3: "http://preview.traxsource.com/files/previews/88/1324290-p.mp3",
              waveform: "http://static.traxsource.com/files/wf/1324290-wf.png",
              bpm: "120",
              keysig: "Bmin"}
    ] ]]></data></root>
    XML;
    
    function javascript_decode($json, $assoc = FALSE){
      $json = str_replace(array("\n","\r"),"",$json);
      $json = preg_replace('(([{,]+)(\s*)([^"]+?)\s*:)','$1"$3":',$json);
      return json_decode($json,$assoc);
    } 
    
    $dom = new DOMDocument();
    //$dom->load($xmlFile);
    $dom->loadXml($xml);
    $xpath = new DOMXpath($dom);
    
    $json = javascript_decode($xpath->evaluate('string(/root/data)'));
    var_dump($json);
    

    输出https://eval.in/105256

    array(1) {
      [0]=>
      object(stdClass)#3 (21) {
        ["title_id"]=>
        string(6) "284270"
        ["track_id"]=>
        string(7) "1548617"
        ["artist"]=>
        array(1) {
          [0]=>
          array(4) {
            [0]=>
            int(20670)
            [1]=>
            int(1)
            [2]=>
            string(13) "Matthias Vogt"
            [3]=>
            string(13) "matthias-vogt"
          }
        }
        ["title"]=>
        string(16) "The Wobble Track"
        ["title_url"]=>
        string(30) "/title/284270/the-wobble-track"
        ["track_url"]=>
        string(31) "/track/1548617/the-wobble-track"
        ["label"]=>
        array(3) {
          [0]=>
          int(88)
          [1]=>
          string(11) "Large Music"
          [2]=>
          string(11) "large-music"
        }
        ["genre"]=>
        string(10) "Deep House"
        ["genre_url"]=>
        string(20) "/genre/13/deep-house"
        ["catnumber"]=>
        string(6) "LAR181"
        ["promo"]=>
        bool(false)
        ["duration"]=>
        string(4) "5:54"
        ["r_date"]=>
        string(10) "2014-02-17"
        ["price"]=>
        object(stdClass)#4 (2) {
          ["hbr"]=>
          float(1.99)
          ["wav"]=>
          float(2.74)
        }
        ["bought"]=>
        bool(false)
        ["image"]=>
        string(58) "http://static.traxsource.com/files/images/271306_large.jpg"
        ["thumb"]=>
        string(63) "http://static.traxsource.com/scripts/image.php/44x44/271306.jpg"
        ["mp3"]=>
        string(61) "http://preview.traxsource.com/files/previews/88/1324290-p.mp3"
        ["waveform"]=>
        string(52) "http://static.traxsource.com/files/wf/1324290-wf.png"
        ["bpm"]=>
        string(3) "120"
        ["keysig"]=>
        string(4) "Bmin"
      }
    }
    

    【讨论】:

    • 谢谢,但是 var_dump 只是给了我 NULL,我错过了什么吗?
    • 如果未加载 XML 或 JSON 无效,则可能发生这种情况。我编辑了答案并添加了示例数据和实时链接。
    • 您能否使用我的问题中包含的实际数据来完成这项工作?还是该数据格式不正确?如果没有,那么您对如何解决我的问题有任何想法吗?
    • 不完整的?不,JSON当然不能被解码,它是不完整的。但是,XML 读取部分可以使用它:eval.in/103112
    • 好的,但我仍然不清楚你的答案如何解决我遇到的问题......?
    【解决方案2】:

    鉴于我无法用我不断挖掘并进行一些测试的任何解决方案来解决问题。我的解决方案并不漂亮,但肯定是实用的。 我删除了所有的 XML 数据,给自己留下了相当原始的 JSON。在这种情况下,JSON 数据的问题是字符串没有包含在 "" 中,所以我做了一个 str_replace 来解决这个问题,这一切都奏效了。 $content= str_replace('','',str_replace("\'","'",$content)));

    $keys = array("title_id:",
    "track_id:",
    "artist:",
    "title:",
    "title_url:",
    "track_url:",
    "label:",
    "genre:",
    "genre_url:",
    "catnumber:",
    "promo:",
    "duration:",
    "r_date:",
    "price:",
    "hbr:",
    "wav:",
    "bought:",
    "false",
    "image:",
    "thumb:",
    "mp3:",
    "waveform:",
    "bpm:",
    "keysig:"
    );
    $newkeys = array("\"title_id\":",
    "\"track_id\":",
    "\"artist\":",
    "\"title\":",
    "\"title_url\":",
    "\"track_url\":",
    "\"label\":",
    "\"genre\":",
    "\"genre_url\":",
    "\"catnumber\":",
    "\"promo\":",
    "\"duration\":",
    "\"r_date\":",
    "\"price\":",
    "\"hbr\":",
    "\"wav\":",
    "\"bought\":",
    "\"false\"",
    "\"image\":",
    "\"thumb\":",
    "\"mp3\":",
    "\"waveform\":",
    "\"bpm\":",
    "\"keysig\":"
    );
    $content=  str_replace($keys, $newkeys, $content);
    $json = json_decode($content, true);
    

    然后我可以循环并正常处理。

    【讨论】:

      猜你喜欢
      • 2012-02-27
      • 2010-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-03
      • 1970-01-01
      • 1970-01-01
      • 2021-08-08
      相关资源
      最近更新 更多