PHP SimpleXML +获取属性 [英] PHP SimpleXML + Get Attribute

查看:68
本文介绍了PHP SimpleXML +获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取的XML看起来像这样:

The XML I am reading looks like this:

<show id="8511">

    <name>The Big Bang Theory</name>
    <link>http://www.tvrage.com/The_Big_Bang_Theory</link>
    <started>2007-09-24</started>
    <country>USA</country>

    <latestepisode>
        <number>05x23</number>
        <title>The Launch Acceleration</title>
    </latestepisode>

</show>

要获取(例如)最新剧集的编号,我会这样做:

To get (for example) The number of the latest episode, I would do:

$ep = $xml->latestepisode[0]->number;

这很好用.但是我该怎么做才能从<show id="8511">获取ID?

This works just fine. But what would I do to get the ID from <show id="8511"> ?

我尝试过类似的事情:

$id = $xml->show;
$id = $xml->show[0];

但没有一个.

更新

我的代码段:

$url    = "http://services.tvrage.com/feeds/episodeinfo.php?show=".$showName;
$result = file_get_contents($url);
$xml = new SimpleXMLElement($result);

//still doesnt work
$id = $xml->show->attributes()->id;

$ep = $xml->latestepisode[0]->number;

echo ($id);

Ori. XML:

http://services.tvrage.com/feeds/episodeinfo.php?show=The.Big.Bang.Theory

推荐答案

这应该有效.

$id = $xml["id"];

您的XML根目录成为SimpleXML对象的根目录;您的代码正在使用名称"show"来调用chid根,但该根不存在.

Your XML root becomes the root of the SimpleXML object; your code is calling a chid root by the name of 'show', which doesn't exist.

您也可以在一些教程中使用此链接: http://php. net/manual/en/simplexml.examples-basic.php

You can also use this link for some tutorials: http://php.net/manual/en/simplexml.examples-basic.php

这篇关于PHP SimpleXML +获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆