PHP SimpleXML + 获取属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10537657/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP SimpleXML + Get Attribute
提问by r0skar
The XML I am reading looks like this:
我正在阅读的 XML 如下所示:
<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;
This works just fine. But what would I do to get the ID from <show id="8511">?
这工作得很好。但是我该怎么做才能从中获取 ID <show id="8511">?
I have tried something like:
我尝试过类似的事情:
$id = $xml->show;
$id = $xml->show[0];
But none worked.
但没有一个奏效。
Update
更新
My code snippet:
我的代码片段:
$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:
奥利。XML:
http://services.tvrage.com/feeds/episodeinfo.php?show=The.Big.Bang.Theory
回答by Kneel-Before-ZOD
This should work.
这应该有效。
$id = $xml["id"];
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.
您的 XML 根成为 SimpleXML 对象的根;您的代码正在以不存在的“show”名称调用 chid 根。
You can also use this link for some tutorials: http://php.net/manual/en/simplexml.examples-basic.php
您也可以使用此链接获取一些教程:http: //php.net/manual/en/simplexml.examples-basic.php
回答by Rawkode
You need to use attributes
您需要使用属性
I believe this should work
我相信这应该有效
$id = $xml->show->attributes()->id;
回答by Mudassar Ali Sahil
This should work. You need to use attributes with type (if sting value use (string))
这应该有效。您需要使用带有类型的属性(如果使用 sting 值(字符串))
$id = (string) $xml->show->attributes()->id;
var_dump($id);
Or this:
或这个:
$id = strip_tags($xml->show->attributes()->id);
var_dump($id);
回答by Rocket Hazmat
You need to use attributes()to get the attributes.
您需要使用attributes()来获取属性。
$id = $xml->show->attributes()->id;
You can also do this:
你也可以这样做:
$attr = $xml->show->attributes();
$id = $attr['id'];
Or you can try this:
或者你可以试试这个:
$id = $xml->show['id'];
Looking at the edit to your question (<show>is your root element), try this:
查看对您的问题的编辑(<show>是您的根元素),试试这个:
$id = $xml->attributes()->id;
OR
或者
$attr = $xml->attributes();
$id = $attr['id'];
OR
或者
$id = $xml['id'];
回答by Rocket Hazmat
try this
尝试这个
$id = (int)$xml->show->attributes()->id;
回答by Baba
You need to format your XMLproperly and let it have examply using <root></root>or <document></document>anything .. see XML specification and examples at http://php.net/manual/en/function.simplexml-load-string.php
您需要XML正确格式化并让它具有例如使用<root></root>或<document></document>任何东西.. 请参阅http://php.net/manual/en/function.simplexml-load-string.php 上的XML 规范和示例
$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);
回答by aguante
After you have correctly load the xml file using the SimpleXML objecto you can do a print_r($xml_variable)and you can easily find which attributes you can access. As other users said $xml['id']also worked for me.
使用 SimpleXML objecto 正确加载 xml 文件后,您可以执行以下操作print_r($xml_variable),您可以轻松找到可以访问的属性。正如其他用户所说,$xml['id']也对我有用。

