0

我有这个

<rss>
 <channel>
  <item>
   <link>http://www1</link>
   <title>Doe </title>
   <description>Para  </description>
  </item>
 </channel>
</rss>

和这个

<feed>
 <article>
   <link>http://www1</link>
   <title>Doe </title>
   <description>Para  </description>
 </article>
</feed>

如何使用相同的代码来处理这两种 xml。

我试试这个但不起作用

$xml_root=$_GET['xml_root'];    // $xml_root='channel'
$xml_item=$_GET['xml_item'];    // $xml_item='item'
$xml= file_get_contents($xmlfile);
$xmldoc = new SimpleXmlElement($xml);
foreach ($xmldoc->$xml_root->$xml_item as $xmltree){

I received this: Warning: Invalid argument supplied for foreach() in

I try use

foreach ($xmldoc->$xml_root()->$xml_item() as $xmltree){

but doesn't work too

请帮帮我!!

4

1 回答 1

0

尝试simplexml_load_file

$xmlfile = "rss.xml"; // or feed.xml

$xml= simplexml_load_file($xmlfile);

$array = $xml->channel->item ? $xml->channel->item : $xml->article;

foreach($array as $value) {
    echo $value->link;
}
于 2013-09-03T13:45:34.797 回答