Adam Waite
Adam Waite

Reputation: 18855

Parse XML link with PHP

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

Upvotes: 0

Views: 5774

Answers (5)

soju
soju

Reputation: 25312

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/>';
}

Upvotes: 0

David
David

Reputation: 101

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

Upvotes: 3

Martin
Martin

Reputation: 6015

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();
    }
}

Upvotes: 1

Churk
Churk

Reputation: 4637

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"}; https://www.php.net/manual/en/class.simplexmlelement.php

Upvotes: 1

enygma
enygma

Reputation: 684

Try this:

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

Then see if link comes through...

Upvotes: -3

Related Questions