2014-02-19 38 views
-1

雖然有很多的選項,將「正常」 XML到一個數組我十分想找到這個數據轉換到一個數組的方式我可以用PHP程序(它目前旨在通過JQuery的處理)如何以下XML數據轉換成一個陣列(JSON或其他方式)

<?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> 

大約有另一個99個物體在該XML字符串,所以我只包括1爲簡單起見

我想轉換,看起來是什麼,將數組插入JSON或PHP數組中 - 感謝:)

回答

-1

既然我不能與任何我不停地挖,並做了一些測試的解決方案解決問題。我的解決方案並不漂亮,但肯定功能。 我刪除了所有的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); 

我然後能夠循環並正常處理。

0

不要。只需使用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" 
    } 
} 
+0

謝謝,但var_dump只是給了NULL,我錯過了什麼? –

+0

如果XML未加載或JSON無效,則可能發生這種情況。我編輯了答案並添加了示例數據和實時鏈接。 – ThW

+0

您是否可以使用包含在我的問題中的實際數據進行工作?或者數據格式不正確?如果沒有,那麼你有什麼想法如何解決我的問題? –

相關問題