19

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
r0skar
  • 8,338
  • 14
  • 50
  • 69

7 Answers7

33

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

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
Kneel-Before-ZOD
  • 4,141
  • 1
  • 24
  • 26
  • 2
    This seems to do the trick! Thanks a lot! (I should probably take more time to look at the examples next time :( ) – r0skar May 10 '12 at 16:03
12

You need to use attributes

I believe this should work

$id = $xml->show->attributes()->id;
Rawkode
  • 21,990
  • 5
  • 38
  • 45
  • This would not work ... the `show` is the default root ... he need to warp the `xml` properly – Baba May 10 '12 at 15:56
  • Unfortunately I keep getting `Warning: main() [function.main]: Node no longer exists in....` – r0skar May 10 '12 at 15:57
9

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);
Patrick Kostjens
  • 5,065
  • 6
  • 29
  • 46
7

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'];
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • The first and second example unfortunately output `Warning: main() [function.main]: Node no longer exists in....`, while the last one doesnt show anything at all. – r0skar May 10 '12 at 15:58
  • It works, let me know in case you did edit @Sam´s answer too, because than I´d change the correct answer and choose yours now. – r0skar May 10 '12 at 16:05
  • @Andrej: Nope Sam's answer was like that from the start :-P I just edited the formatting. – gen_Eric May 10 '12 at 16:06
3

try this

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

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);
Baba
  • 94,024
  • 28
  • 166
  • 217
0

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.

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
aguante
  • 35
  • 2