0

我正在使用 ANN api 提取信息。我使用 simpleXML 类来获取信息。

function explodeTest() {
            $result = $this->_restGenerator($this->ann, '6236');
            //print_r($result);
            foreach ($result->anime as $data) {
                print_r($data->info);
            } 
        }

_restGenerator 函数:

function _restGenerator($url,$q) {
        $url = $url . strtolower($q);
        if(strlen($q) > 0) {
            $q = file_get_contents($url);
            //$q = simplexml_load_file($url)
            return simplexml_load_string($q);
        }
    }

常数:

$this->ann = 'http://cdn.animenewsnetwork.com/encyclopedia/api.xml?anime=';

当我运行该函数时,我得到了这个:

SimpleXMLElement Object ( [@attributes] => Array ( [gid] => 3506750509 [type] => Picture [src] => http://cdn.animenewsnetwork.com/thumbnails/fit200x200/encyc/A6236-259.jpg ) )

但实际的 XML 标记显示为:

<anime id="6236" gid="1601610894" type="TV" name="Gintama" precision="TV" generated-on="2012-07-07T04:58:39Z">
   <info gid="3506750509" type="Picture" src="http://cdn.animenewsnetwork.com/thumbnails/fit200x200/encyc/A6236-259.jpg"/>
   <info gid="1229282479" type="Main title" lang="JA">Gintama</info>
   <info gid="556528412" type="Alternative title" lang="RU">Гинтама</info>
   <info gid="1383170257" type="Alternative title" lang="JA">銀魂&lt;/info>
   <info gid="380581077" type="Alternative title" lang="KO">은혼&lt;/info>
   <info gid="3483398015" type="Genres">action</info>
   <info gid="1567209986" type="Genres">adventure</info>
   <info gid="1221683927" type="Genres">comedy</info>
   <info gid="3139902810" type="Genres">drama</info>
   <info gid="2565080252" type="Genres">fantasy</info>
   <info gid="971885680" type="Genres">science fiction</info>
   <info gid="2312087995" type="Objectionable content">TA</info>
   <info gid="1950277303" type="Plot Summary">...</info>
   <info gid="2741727987" type="Running time">25</info>
   <info gid="3466023682" type="Number of episodes">201</info>
   <info gid="2618069239" type="Vintage">2006-04-04 to 2010-03-25</info>
   <info gid="820682777" type="Vintage">2007-12-04 (Italia, MTV Italia)</info>
   <info gid="2490517434" type="Vintage">2009-02-03 (Spain, Canal Extremadura)</info>
   <info gid="1770356394" type="Vintage">2009-08-30 (Malaysia, TV2)</info>
   <info gid="2362558760" type="Vintage">2010-01-18 (Philippines, ABS-CBN - Team Animazing)</info>
   <info gid="803932272" type="Vintage">2011-03-23 (Philippines - Hero, League of Heroes)</info>
   <info gid="2236361640" type="Vintage">2011-04-04</info>
   <info gid="1161326503" type="Opening Theme">#1: "Pray" by Tommy Heavenly6 (eps 1-24)</info>
   <info gid="2241431608" type="Opening Theme">#2: "Tooi Nioi" by YO-KING (eps 25-49)</info>
   <info gid="1855414862" type="Opening Theme">
#3: "Gin-iro no Sora" (銀色の空; "Silver Sky") by redballoon (eps 50-)
</info> ...

当我做 print_r($result) ...信息节点显示为

[info] => Array ( 
                    [0] => SimpleXMLElement Object ( 
                            [@attributes] => Array ( 
                                                [gid] => 3506750509 
                                                [type] => Picture 
                                                [src] => http://cdn.animenewsnetwork.com/thumbnails/fit200x200/encyc/A6236-259.jpg ) 
                                                ) 
                    [1] => Gintama 
                    [2] => Гинтама 
                    [3] => 銀魂 
                    [4] => ì€í˜¼ 
                    [5] => action 
                    [6] => adventure 
                    [7] => comedy 
                    [8] => drama 
                    [9] => fantasy 
                    [10] => science fiction 
                    [11] => TA 
                    [12] => Twenty years ago .. <synopsis>
                    [13] => 25 
                    [14] => 201 

我需要获取节点的类型属性以使数组可区分,或者至少按类型属性过滤信息节点。

谢谢。

4

1 回答 1

1

使用attributes 函数获取属性。

foreach ($result->anime as $data) {
    foreach($data->info as $info){
        $a = $info->attributes();
        echo $a['type']. "\n";
    }
} 
于 2012-07-07T07:04:35.870 回答