PHP SimpleXML + Get Attribute

独自空忆成欢 提交于 2019-12-17 09:49:53

问题


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;

This works just fine. But what would I do to get the ID from <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:

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

回答1:


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.

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




回答2:


You need to use attributes

I believe this should work

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



回答3:


This should work. You need to use attributes with type (if sting value use (string))

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

Or this:

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



回答4:


You need to use attributes() to get the 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:

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

OR

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

OR

$id = $xml['id'];



回答5:


try this

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



回答6:


You need to format your XML properly 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 = '<?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);



回答7:


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.



来源:https://stackoverflow.com/questions/10537657/php-simplexml-get-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!