我正在写这篇文章因为我试图解决一个似乎很愚蠢的问题,但不幸的是我无法做到。我有一个XML文件(链接),我正在解析simplexml_load_string($ link),如下所示:
$response_xml_data = file_get_contents($link);
$xml = simplexml_load_string($response_xml_data);
foreach ($xml->item as $licenseElement) {
$title = $licenseElement->title;
echo $title;
}
我需要获得title
和link
的价值,但没有任何结果。
我尝试打印$link
并告诉我:
SimpleXMLElement Object
(
[@attributes] => Array
(
[version] => 2.0
)
[channel] => SimpleXMLElement Object
(
[title] => GamePlay
[link] => https://example.com/
[description] => The latest blog posts from example.com.
[language] => en-us
[pubDate] => Mon, 14 Nov 2016 18:50:50 +0000
[lastBuildDate] => Mon, 14 Nov 2016 18:50:50 +0000
[managingEditor] => info@example.com
[item] => SimpleXMLElement Object
(
[title] => IO Interactive
[link] => http://example.com/?p=258
)
)
)
更新 当尝试var_dump($ xml);在循环之前:
object(SimpleXMLElement)#2568 (2) {
["@attributes"]=>
array(1) {
["version"]=>
string(3) "2.0"
}
["channel"]=>
object(SimpleXMLElement)#2571 (8) {
["title"]=>
string(8) "Gameplay"
["link"]=>
string(21) "https://example.com/"
["description"]=>
string(40) "The latest blog posts from example.com."
["language"]=>
string(5) "en-us"
["pubDate"]=>
string(31) "Mon, 14 Nov 2016 19:08:33 +0000"
["lastBuildDate"]=>
string(31) "Mon, 14 Nov 2016 19:08:33 +0000"
["managingEditor"]=>
string(17) "info@example.com"
["item"]=>
object(SimpleXMLElement)#2574 (2) {
["title"]=>
string(76) "IO Interactive "
["link"]=>
string(217) "http://example.com/?p=258"
}
}
}