第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

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

使用 PHP 從新聞網(wǎng)站訪問 XML 元素和名稱空間

使用 PHP 從新聞網(wǎng)站訪問 XML 元素和名稱空間

炎炎設計 2023-10-04 14:23:48
file_get_contents我使用和 嘗試檢索 xml 內(nèi)容SimpleXMLElement,但我得到的 PHP 數(shù)組僅包含 XML 數(shù)據(jù)的父元素。使用時simplexml_load_string我得到這個PHP 數(shù)組:SimpleXMLElement Object(    [url] => Array        (            [0] => SimpleXMLElement Object                (                    [loc] => https://www.lemonde.fr/culture/article/2020/03/22/crise-sanitaire-malgre-les-annonces-du-gouvernement-les-intermittents-du-spectacle-restent-inquiets_6034031_3246.html                    [lastmod] => 2020-03-12T20:00:12+01:00                )            [1] => SimpleXMLElement Object                (                    [loc] => https://www.lemonde.fr/climat/article/2020/03/22/l-eau-a-l-epreuve-des-changements-climatiques_6034029_1652612.html                    [lastmod] => 2020-03-22T16:34:35+01:00<                )            [2] => SimpleXMLElement Object                (                    [loc] => https://www.lemonde.fr/journal-blouses-blanches/article/2020/03/22/journal-de-crise-des-blouses-blanches-la-consigne-est-de-se-cacher-quand-le-brancard-passe_6034028_6033712.html                    [lastmod] => 2020-03-22T16:02:29+01:00                )我需要檢索特定元素,那么如何才能訪問它們呢?提前致謝!
查看完整描述

1 回答

?
GCT1015

TA貢獻1827條經(jīng)驗 獲得超4個贊

您可以使用 XPath 進入文檔:


$xml = simplexml_load_string($content);

$xml->registerXPathNamespace('s', 'http://www.sitemaps.org/schemas/sitemap/0.9'); // xpath need to have an 'alias' to query the anonymous namespace


$urls = $xml->xpath('//s:url'); // retrieve all url items


foreach($urls as $url) // loop over each url item

{

    $url->registerXPathNamespace('s', 'http://www.sitemaps.org/schemas/sitemap/0.9');


    // string conversion gives you only the content of the node

    $title = (string) $url->xpath('news:news/news:title')[0];

    $loc = (string) $url->xpath('s:loc')[0];

    $lastmod = (string) $url->xpath('s:lastmod')[0];

    $pubDate = (string) $url->xpath('news:news/news:publication_date')[0] ;


    echo $title . PHP_EOL; 

    echo $loc . PHP_EOL ; 

    echo $lastmod . PHP_EOL ; 

    echo $pubDate . PHP_EOL; 

}

//意味著您查看文檔中任何位置的所有具有該名稱的節(jié)點,single/意味著您查看該節(jié)點的直接子節(jié)點。您可以閱讀 XPath 的文檔來了解更多命令。


XPath 返回節(jié)點的集合,因此我總是查詢第一個元素[0](假設文檔中只有一個這樣的元素)


查看完整回答
反對 回復 2023-10-04
  • 1 回答
  • 0 關注
  • 133 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號