【问题标题】:PHP Parse XML with simplexml_load_filePHP 使用 simplexml_load_file 解析 XML
【发布时间】:2015-10-06 08:52:58
【问题描述】:

解析如下所示的 XML 提要的方法是什么:

<string xmlns="http://vs-social-feed/">
{ "response": "success", "message": "", "feed":
[
{"SocialId":105,"Type":2,"Id":"202297433163449_956836567709528","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3","Text":"Hold your tongue!\n\nStacy Martin by Ellen von Unwerth official for Vs. Magazine","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956836567709528/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"\/Date(1444068001000)\/"},
{"SocialId":104,"Type":2,"Id":"202297433163449_956833817709803","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3","Text":"Proper posture, ladies!\n\nThe always elegant, Christy Turlington Burns by Patrick Demarchelier","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956833817709803/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"\/Date(1444075201000)\/"}
]
}
</string>

我尝试过使用 simplexml_load_file、file_get_contents、json_decode 和 json 编码,但没有任何运气。

【问题讨论】:

  • 认为是 XML,因为 Feed 被包裹在 social-feed"> ...
  • 您将首先从 XML 中获得 JSON 值,然后对其进行解码。

标签: php xml


【解决方案1】:

它是 JSON 数据而不是 XML。 PHP 内置了 json_decode() 函数来解析 json 数据。

试试这个代码:

<?php

$data = '{ "response": "success", "message": "", "feed":[{"SocialId":105,"Type":2,"Id":"202297433163449_956836567709528","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3","Text":"Hold your tongue!\n\nStacy Martin by Ellen von Unwerth official for Vs. Magazine","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956836567709528/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956836567709528/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"/Date(1444068001000)/"},{"SocialId":104,"Type":2,"Id":"202297433163449_956833817709803","Url":"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3","Text":"Proper posture, ladies!\n\nThe always elegant, Christy Turlington Burns by Patrick Demarchelier","Other":"\u003ca href=\"https://www.facebook.com/vsmag/photos/a.209641845762341.52114.202297433163449/956833817709803/?type=3\" target=\"_blank\"\u003e\u003cimg src=\"https://graph.facebook.com/956833817709803/picture/\" \" width=\"220\"\u003e\u003c/a\u003e","DateCreate":"/Date(1444075201000)/"}] }';
$obj = json_decode($data);
echo "<pre>"; print_r($obj->feed); echo "</pre>";

?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 1970-01-01
    相关资源
    最近更新 更多