0

I have an XML feed that looks something like this:

xml

I can parse the title easily enough using simpleXML:

$pictureBoxXMLFeed = simplexml_load_file('https://www.picturebox.tv/xml/feeds/FindAnyFilm/FindAnyFilm.xml');;

echo $pictureBoxXMLFeed->entry[1]->title;

foreach($pictureBoxXMLFeed->entry as $value){

    echo $value->title;
    echo '<br/>';

}

But I need to grab the link element in the feed which looks something like this:

<link href="http://www.picturebox.tv/watchnow?id=UKIC30" rel="alternate"/>

FYI, this doesn't work:

echo $value->link;

Thanks for any help...

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
  • Define "doesn't work" - does it just not show anything or does it throw an error? – enygma Mar 09 '12 at 13:12
  • doesn't show anything, it highlights as echo does in DreamWeaver leading me to believe it's reserved? – Adam Waite Mar 09 '12 at 13:12
  • If the root element of the XML is entry, then you don't need to address it. What happens with echo $pictureBoxXMLFeed->link->attributes()->href; – Martin Mar 09 '12 at 13:15
  • 2
    `$value->link` works perfectly. The *element* is empty though, so what should the output be? If you are after the href *attribute*, then a look at [SimpleXml Example #5 in the PHP Manual](http://www.php.net/manual/en/simplexml.examples-basic.php) will tell you need to use `$value->link['href'];` – Gordon Mar 09 '12 at 13:19
  • 1
    possible duplicate of [How to get attribute with SimpleXml](http://stackoverflow.com/questions/3410520/how-to-get-an-attribute-with-simplexml) – Gordon Mar 09 '12 at 13:23

5 Answers5

3

Is this what you mean?

$string = '
<entry>
    <link href="http://www.picturebox.tv/watchnow?id=UKIC30" rel="alternate"/>
</entry>';

$simpleXML = simplexml_load_string($string);
foreach($simpleXML->link->attributes() as $name => $value) {
    echo $name.': '.$value.'<br />';
}

Gives:

href: http://www.picturebox.tv/watchnow?id=UKIC30
rel: alternate
David
  • 101
  • 2
1

In each of the $value, it is a simplexml_element, you an href is an attribute, so you need to do a check on

foreach ($value->attributes as $a) {
  if ($a->getName() == "href") { do something; }
}

or $value->{"href"}; http://us2.php.net/manual/en/class.simplexmlelement.php

Churk
  • 4,556
  • 5
  • 22
  • 37
1

How about this?

$pictureBoxXMLFeed = simplexml_load_file('https://www.picturebox.tv/xml/feeds/FindAnyFilm/FindAnyFilm.xml');;

foreach($pictureBoxXMLFeed->entry[1] as $value){
    if($value->getName() == 'link') {
    echo $value->asXML();
    }
}
Martin
  • 5,945
  • 7
  • 50
  • 77
0

href is an attribute, so :

foreach($pictureBoxXMLFeed->entry as $value){
    echo $value->link['href'];
    echo '<br/>';
}

or

foreach($pictureBoxXMLFeed->entry as $value){
    echo $value->link->attributes()->href;
    echo '<br/>';
}
soju
  • 25,111
  • 3
  • 68
  • 70
-3

Try this:

$pictureBoxXMLFeed = simplexml_load_file('https://www.picturebox.tv/xml/feeds/FindAnyFilm/FindAnyFilm.xml',LIBXML_NOEMPTYTAG);

Then see if link comes through...

enygma
  • 684
  • 4
  • 6
  • 1
    The file's being loaded into Simple XML just fine. He's trying to retrieve an attribute through incorrect access. – MetalFrog Mar 09 '12 at 13:23