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 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,