douji6199 2013-05-08 04:05 采纳率: 100%
浏览 33
已采纳

基于值解析XML元素属性

I have no problem parsing many different kinds of XML feeds and have methods for each different situation. I have come across one I am not familiar with how to attack it.

The feed has element attributes which is not a big deal but some of the attributes of the element are like this where the output is based on a value.

tornado="0" funnelcloud="0" wallcloud="0" rotation="0" hail="1" wind="0" flood="0" flashflood="0" other="0"

So basically if there is a 1 it will output that data. So how do I parse that so it outputs the listed one? I am thinking I need an array maybe? If so any examples so I can get an idea?

Here is how I parse the data which works great. Lightweight and simple.

$data = "http://www.spotternetwork.org/data.php";
$xml = simplexml_load_file($data);

foreach($xml->report as $report){

    $date = $report['stamp'];
    $narrative = $report['narrative'];
    $loc = $report['city1'];
    $tz = $report['tz'];

    $time = strtotime($date.' UTC');
    $dateInLocal = date("D g:i a", $time);

-Thanks

  • 写回答

1条回答 默认 最新

  • dqqt31923 2013-05-08 04:15
    关注

    You'll need to specify what you are using to parse the xml but assuming it's simpleXML, you could do something like:

    $attrs = [];
    foreach($report->attributes() as $a => $b) {
        $attrs[$a] = $b;
    }
    
    $value = array_search('1', $attrs);
    

    Switch:

    switch ($value) {
        case 'hail':
            echo "hail stuff";
            break;
        case 'tornado':
            echo "tornado stuff";
            break;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程