Accessing namespace colon nodes in XML with SimpleXML PHP

前端 未结 3 775
梦如初夏
梦如初夏 2020-12-22 08:11

This is similar to a question I\'ve posted, but I\'ve broadened out as this has got to be solvable.

I am trying to access rating and viewCount from this rss feed. No

3条回答
  •  独厮守ぢ
    2020-12-22 08:49

    Using DomDocument and DomXpath:

    error_reporting(E_ALL ^ E_STRICT);
    ini_set('display_errors', 'on');
    
    $dom = new DomDocument;
    $dom->load('data.xml');
    
    $xpath = new DomXpath($dom);
    $xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom');
    $xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/');
    $xpath->registerNamespace('gd', 'http://schemas.google.com/g/2005');
    $xpath->registerNamespace('yt', 'http://gdata.youtube.com/schemas/2007');
    
    $favoriteCountAttr = $xpath->query('/atom:entry/yt:statistics/@favoriteCount')->item(0);
    if ($favoriteCountAttr instanceof DomAttr) {
        echo $favoriteCountAttr->nodeValue; // output: 10
    }
    

提交回复
热议问题