<?php
// load SimpleXML
$entry = new SimpleXMLElement('http://bit.ly/c3IqMF', null, true);
echo <<<EOF
<table>
<tr>
<th>Title</th>
<th>Image</th>
</tr>
EOF;
foreach($entry as $item) //
{
echo <&开发者_C百科lt;<EOF
<tr>
<td>{$item->title}</td>
<td><img src="{$item->children('im', true)->image}"></td>
</tr>
EOF;
}
echo '</table>';
?>
The above php works but somehow, I got 8 empty table entities above the result
<tr>
<td></td>
<td><img src=""></td>
</tr>
What's wrong with the code? How do I get rid of the empty table entities?
The way you have it now it gets the <id>, <title>, <updated>
from the the start of the xml. Actually you needed all the entry
entries in the xml. So it should be $entry->entry
foreach($entry->entry as $item) //
{
echo <<<EOF
<tr>
<td>{$item->title}</td>
<td><img src="{$item->children('im', true)->image}"></td>
</tr>
EOF;
}
Honestly, I think you are approaching this the wrong way. Since it seems that you are trying to parse an Atom feed, try using something designed for that, like Magpie RSS. It will probably save you a lot of time.
精彩评论