Reputation: 2363
I'm trying to retrieve the the HREF and echo it out using SimpleXML, but I keep getting the error message below.
Warning: Invalid argument supplied for foreach() in ...
<?php
$url = 'file.xml';
foreach(simplexml_load_file($url)->info->content->item as $it) {
echo $it->site-page-href;
}
?>
<?xml version="1.0" encoding="utf-8"?>
<info>
<content>
<item>
<site>
<page>
<href>http://domain.com</href>
</page>
</site>
</item>
</content>
<info>
Can anyone spot the problem?
Upvotes: 0
Views: 774
Reputation: 11395
echo $it->site-page-href;
should be echo
$it->site->page->href;
</info>
->info
as you
are already at the root element.simplexml_load_file($url)->content->item
notsimplexml_load_file($url)->info->content->item
Btw, finding all of the href nodes can be achieved more easily if you use xpath.
$xml = simplexml_load_file($url);
$href = $xml->xpath(".//href");
foreach($href as $h) {
var_dump($h);
}
Upvotes: 3
Reputation: 668
Firstly use
echo $it->site->page->href;
instead
echo $it->site-page-href;
and end the <info>
tag with </info>
in your XML file.
Upvotes: 0