0

I've this piece of code taken by a .atom document wrapped with file_get_contents:

<entry>
    <link type="text/html" rel="alternate" href="..."/>
</entry>

My goal is to extract the first URL into the href attribute in the entry tag, i tried to parse with:

$xml = new SimpleXMLElement($XML_file);
$link = $xml->entry[0]->link;
print $link;

But shell does not give me any output.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Gabriele Salvatori
  • 460
  • 1
  • 3
  • 20

2 Answers2

1

Did you try? As your link element looks empty.

<entry>
    <link type="text/html" rel="alternate" href="...">Something</link>
</entry>

And than your php?

That is my point, t is exactly what you try to echo. to get attributes

http://www.php.net/manual/en/simplexmlelement.attributes.php

$text = '<entry><link type="text/html" rel="alternate" href="..." /></entry>';
$xml = simplexml_load_string($text);
$linkAttributes = $xml->link->attributes();
foreach ($linkAttributes as $key => $value)  {
    echo $key . '::' . $value . PHP_EOL;
}

outputs:

type::text/html
rel::alternate
href::...
E_p
  • 3,136
  • 16
  • 28
0

When you load an xml document, the document is the root element (in this case <entry>). Therefore, you want:

$xml = new SimpleXMLElement($XML_file);
$link = $xml->link;
print $link;
Eric
  • 95,302
  • 53
  • 242
  • 374