2012-05-10 148 views
18

的XML我正在读看起来像这样:PHP的SimpleXML +获取属性

<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> 

要获得(例如)在最新一集的数量,我会做:

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

该作品正好。但是,如何从<show id="8511">获得ID?

我已经试过类似:

$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); 

大利。 XML:

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

http://php.net/ simplexml.examples-basic – hakre

+0

可能的重复[从SimpleXML访问@attribute](http://stackoverflow.com/questions/1652128/accessing-attribute-from-simplexml) - 供参考。 – hakre

+0

请参阅: http://stackoverflow.com/questions/10537657/php-simplexml-get-attribute/19289857#19289857 –

回答

31

这应该工作。

$id = $xml["id"]; 

您的XML根成为SimpleXML对象的根;你的代码通过'show'的名字来调用一个chid root,这个名字不存在。

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

+2

这似乎是个窍门!非常感谢! (我应该花更多的时间来看看下一个例子:() – Andrej

12

您需要使用attributes

我认为这应该工作

$id = $xml->show->attributes()->id; 
+0

这不会工作......'show'是默认的根...他需要扭曲(xml) – Baba

+0

不幸的是,我不断收到警告:main()[function.main]:节点不再存在于....' – Andrej

7

您需要使用attributes()得到的属性。

$id = $xml->show->attributes()->id; 

你也可以这样做:

$attr = $xml->show->attributes(); 
$id = $attr['id']; 

或者你可以试试这个:

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

望着编辑您的问题(<show>是你的根元素),试试这个:

$id = $xml->attributes()->id; 

$attr = $xml->attributes(); 
$id = $attr['id']; 

OR

$id = $xml['id']; 
+0

第一个和第二个例子不幸输出'Warning:main()[function.main ]:节点不再存在....',而最后一个不显示任何东西。 – Andrej

+0

@Andrej:检查我的编辑。 –

+0

它的作品,让我知道,如果你也编辑@山姆的答案,因为我不会改变正确的答案,并选择你的现在。 – Andrej

0

您需要正确地格式化XML,让它有examply使用<root></root><document></document>什么..看到XML规范和实例在http://php.net/manual/en/function.simplexml-load-string.php

$xml = '<?xml version="1.0" ?> 
<root> 
<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> 
</root>'; 

$xml = simplexml_load_string ($xml); 
var_dump ($xml->show->attributes()->id); 
+0

不幸的是,我已经没有影响如何格式化xml! – Andrej

+0

我将原始XML源添加到我的文章! – Andrej

0

您正确加载使用的SimpleXML objecto你可以做一个print_r($xml_variable) XML文件后,你可以很容易地找到它的属性,你可以访问。正如其他用户说$xml['id']也为我工作。

8

这应该工作。 您需要使用的属性类型(如果蜇值使用(字符串))

$id = (string) $xml->show->attributes()->id; 
var_dump($id); 

或者这样:

$id = strip_tags($xml->show->attributes()->id); 
var_dump($id); 
3

试试这个

$id = (int)$xml->show->attributes()->id;