dougang1967 2013-09-03 13:39
浏览 33
已采纳

如何使用具有相同功能的两个xml结构

I have this

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

and this

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

How can I use the same code to work with this two kinds of xml.

I try this but doesn't work

$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

Help me Please!!

  • 写回答

1条回答 默认 最新

  • dongnai8013 2013-09-03 13:45
    关注

    Try with 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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算