亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何用 PHP 解析 xml 站點地圖?

如何用 PHP 解析 xml 站點地圖?

PHP
楊__羊羊 2022-12-30 16:11:20
我想打印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


查看完整回答
反對 回復 2022-12-30
  • 1 回答
  • 0 關注
  • 91 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號