SimpleXML不会正确返回数据

时间:2010-07-04 20:05:27

标签: php xml rss simplexml

我正在使用SimpleXML从外部源解析数据文件。我试图从结果中提取缩略图,如下所示:

    <entry>
        <title>Ball_Punch</title>
        <author>
            <name>burningcandle2010</name>
            <uri>https://www.mochimedia.com/community/profile/burningcandle2010</uri>
        </author>
        <link href="http://www.mochimedia.com/games/allout-offsite" rel="alternate" />
        <link href="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" rel="enclosure" type="application/x-shockwave-flash" />
        <id>urn:uuid:bf720e45-7ca0-34c7-a63a-0f6f20a4c267</id>
        <media:player height="470" url="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" width="798" />
        <media:thumbnail height="100" url="http://thumbs.mochiads.com/c/g/allout-offsite/_thumb_100x100.png" width="100" />
        <media:title>Ball_Punch</media:title>
        <media:description>punch your ball</media:description>
        <media:keywords>other, rhythm</media:keywords>
        <category term="Puzzles" />
        <updated>2010-07-04T08:13:22.571963-08:00</updated>
        <published>2010-07-04T06:58:55.577826-08:00</published>
        <summary type="xhtml">
            <div xmlns="http://www.w3.org/1999/xhtml">
            <a href="http://www.mochimedia.com/games/allout-offsite">
                <img class="thumbnail" src="http://thumbs.mochiads.com/c/g/allout-offsite/_thumb_100x100.png" />
            </a>
            <dl>
                <dt>Tag</dt>
                <dd class="tag">cdb41e529fbe39bd</dd>
                <dt>Description</dt>
                <dd class="description">punch your ball</dd>
                <dt>Resolution</dt>
                <dd class="resolution">798x470</dd>
                <dt>Instructions</dt>
                <dd class="instructions" />
                <dt>Key Mappings</dt>
                <dd class="key_mappings" />
                <dt>Control Scheme</dt>
                <dd class="control_scheme">{"fire": "left_mouse", "jump": "space", "movement": "mouse"}</dd>
                <dt>Categories</dt>
                <dd class="categories">Puzzles</dd>
                <dt>Keywords</dt>
                <dd class="keywords">other, rhythm</dd>
                <dt>Rating</dt>
                <dd class="rating">Everyone</dd>
                <dt>Leaderboards</dt>
                <dd class="leaderboards">False</dd>
                <dt>Embed</dt>
                <dd>
                    <code class="embed">&lt;embed src="http://games.mochiads.com/c/g/allout-offsite/Ball_Punch.swf" menu="false" quality="high" width="798" height="470" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /&gt;</code>
                </dd>
                <dt>Slug</dt>
                <dd class="slug">allout-offsite</dd>
                <dt>Featured</dt>
                <dd class="recommended">False</dd>
                <dt>Zip File</dt>
                <dd class="zip_url">http://games.mochiads.com/c/g/allout-offsite.zip</dd>
                <dt>SWF file size</dt>
                <dd class="swf_file_size">184374</dd>
            </dl>
        </div>
    </summary>
</entry>

我的代码在这里:

$thumbnail = $game->summary->div->a->img->attributes()->src;

然而,当我通过print_r($ thumbnail)运行时,我得到:

SimpleXMLElement Object
(
    [0] => DATA_I_WANT
)

无论我做什么,它总是会成为这个或一个空的SimpleXMLElement对象。我试过->src[0], ->src->{'0'}等等无济于事。

2 个答案:

答案 0 :(得分:2)

尝试简单地对字符串进行类型转换:

print_r((string) $thumbnail)

当您尝试实际使用它收集的数据时,SimpleXML习惯会有点阴暗。我习惯于将所有吐出的东西都做成类型。

祝你好运!

答案 1 :(得分:1)

只需转换为字符串。 :

$thumbnail = (string)$game->summary->div->a->img->attributes()->src;