我想打印xml文件元素內的值,但我遇到了問題。這個 xml 站點地圖:<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet type="text/xsl" href="/sitemap.xsl"?> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <url><loc>https://www.example.com/1</loc></url> <url><loc>https://www.example.com/2</loc></url> </urlset>這是我的代碼:$url_sitemap = "file.xml";$xml_sitemap = simplexml_load_file($url_sitemap);foreach($xml_sitemap-> urlset -> url as $url){ $link= $url->loc; print_r($link);}但我有這個錯誤:警告:為 foreach() 提供的參數無效
1 回答

楊魅力
TA貢獻1811條經驗 獲得超6個贊
如果你print_r($xml_sitemap);會看到它不包含urlset:
SimpleXMLElement Object
(
[url] => Array
(
[0] => SimpleXMLElement Object
(
[loc] => https://www.example.com/1
)
[1] => SimpleXMLElement Object
(
[loc] => https://www.example.com/2
)
)
)
然后你可以使用
foreach ($xml_sitemap as $url) {
echo $url->loc;
}
輸出所有locs
- 1 回答
- 0 關注
- 91 瀏覽
添加回答
舉報
0/150
提交
取消