dravpuso44681 2013-04-16 20:33
浏览 30
已采纳

从XML获取多个结果(然后根据关键字匹配丢弃某些结果)

Novice PHP programmer here (if I can even call myself that). I am currently working on a way to loop through an XML document to grab every instance of a child object that does not contain a specific keyword and then display the results. I am able to do the aforementioned but only with the first child object that is found.

Examples Follow ...

Example XML There may be more than one instance of the following and I am trying to get the <title> child element from every instance that does not contain the word "Flood" or the phrase "There are no active watches, warnings or advisories".

<entry>
  <id>http://alerts.weather.gov/cap/wwaatmget.php?x=OHC095&amp;y=0</id>
  <updated>2013-04-16T20:00:01+00:00</updated>
  <author>
  <name>w-nws.webmaster@noaa.gov</name>
  </author>
  <title>There are no active watches, warnings or advisories</title>
  <link href='http://alerts.weather.gov/cap/wwaatmget.php?x=OHC095&amp;y=0'/>
</entry>

Variables

//Lucas Co. Weather
$xml_string = file_get_contents("../../cache/weather_alerts-lucas.xml");
$weather_alerts_lucas = simplexml_load_string($xml_string);
$lucas_alert = ($weather_alerts_lucas->entry->title);
$no_alert =('There are no active watches, warnings or advisories');

Displaying The Results The first if statement determines whether there should be a scroll bar or not and the second if statement determines if a specific county should display information (there are multiple counties but I am only showing one here for simplicity).

if ("$lucas_alert"=="$no_alert" || fnmatch("*Flood*", $lucas_alert))
    {
      //Do Nothing. 
      } else {  
        echo "<div id='emergency_bar'>";
        echo "<span class='scroll_font' ... ...'>"; 
          if ("$lucas_alert"=="$no_alert" || fnmatch("*Flood*", $lucas_alert))
              { 
                //Do Nothing.   
                  } else {
                    echo "Lucas Co. - " . $lucas_alert . " // ";
                  }
        echo "</span>";
        echo "</div>";
      }

If I do it the way it is posted above it will only grab the first result and even if it grabbed multiple instances of <title> it would not display anything if one of the instances had the word "Flood" in it.

Thanks in advance. I'm not looking for someone to write code for me, just some direction.

  • 写回答

1条回答 默认 最新

  • dongxiongshi9952 2013-04-16 21:44
    关注
    $xml = simplexml_load_string($x); // XML in $x
    $no = "There are no active watches, warnings or advisories";
    
    foreach ($xml->xpath("//title") as $title) {
    
        if ($title <> $no && strpos($title,"Flood") === false) echo "$title<br />";
    
    }
    

    see it working: http://codepad.viper-7.com/JVOoke

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请问为什么我配置IPsec后PC1 ping不通 PC2,抓包出来数据包也并没有被加密
  • ¥200 求博主教我搞定neo4j简易问答系统,有偿
  • ¥15 nginx的使用与作用
  • ¥100 关于#VijeoCitect#的问题,如何解决?(标签-ar|关键词-数据类型)
  • ¥15 一个矿井排水监控系统的plc梯形图,求各程序段都是什么意思
  • ¥50 安卓10如何在没有root权限的情况下设置开机自动启动指定app?
  • ¥15 ats2837 spi2从机的代码
  • ¥200 wsl2 vllm qwen1.5部署问题
  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 数学建模数学建模需要