dqwh0109 2016-02-12 00:17
浏览 133
已采纳

simplexml_load_string获取属性

How do I set the name or name1 variable to value of the $event's name attribute? When I step through the code name equals a simplexmlobject with no value and name2 is null.

$name3 = $event->attributes()->name; does not work either.

I am confused on how to use this properly.

$curl = curl_init();

curl_setopt_array($curl, Array(
    CURLOPT_URL            => 'http://odds.smarkets.com/oddsfeed.xml',
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_ENCODING       => 'UTF-8'
 ));

$data = curl_exec($curl);
curl_close($curl);

$xml = simplexml_load_string($data);
$football = array();
foreach($xml->event as $event){
if($event->attributes()->type == "Football match"){
    $att = $event->attributes();
    $name = $att['name'];
    $name2 = $att->attributes()->name;
    $name3 = $event->attributes()->name;
    $football[] = $event;
}
}

foreach($football as $game){
   if($game->attributes()->name == "Sunderland vs. Manchester United"){
     $a = $game;
   }
 }
  • 写回答

2条回答 默认 最新

  • doujiexi1824 2016-02-12 00:57
    关注

    Some of the script works, it goes get the game Sunderland. The part that doesn't make sense are the following:

    $att = $event->attributes(); // gets all the attributes
    // using `$att` then invokes the attributes method
    $name2 = $att->attributes()->name;
    

    You don't need to use ->attributes over $att it already has the attributes.

    If you want to search for that Sunderland game, just remove some it the unneeded parts:

    $xml = simplexml_load_string($data);
    $football = array();
    foreach($xml->event as $event){
        if($event->attributes()->type == "Football match"){
            $football[] = $event; // push foot ball matches
        }
    }
    
    foreach($football as $game){
        // search for sunderland game
        if($game->attributes()->name == "Sunderland vs. Manchester United"){
            $a = $game;
        }
    }
    
    print_r($a); // checker
    

    If you really want to assign that particular name into a variable, just typecast the attribute into (string), ala:

    $name = (string) $event->attributes()->name;
    

    Edit: Just go to the appropriate level using foreach if you have to. Just add an is_array to do some checking. Some levels have a flat one on prices. Some have multiple prices on some offers. Do this checking accordingly. To get you going, here's an example:

    $match = 'Sunderland vs. Manchester United';
    foreach($football as $game){
        if($game->attributes()->name == $match){
            // if that game is found
            foreach($game->market as $market) {
    
                foreach($market->contract as $contract) {
    
                    // offers
                    if(!empty($contract->offers)) {
                        foreach($contract->offers->price as $price) {
                            // single level price
                            if(!is_array($price)) {
                                $decimal = (string) $price->attributes()->decimal;
                                echo $decimal;
                            }
                            // multi leveled price
                            else {
                                foreach($price as $p) {
                                    $decimal = (string) $p->attributes()->decimal;
                                    echo $decimal;
                                }
                            }
    
    
                        }
                    }
                    // end offers
    
                }
    
            }
    
        }
    }
    

    The concept is still the same, use typecasting on the attributes when needed. This should get you going.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 电脑出问题了,说是usbmom注册表没
  • ¥20 需要步骤截图(标签-服务器|关键词-map)
  • ¥50 gki vendor hook
  • ¥15 centos7中sudo命令无法使用
  • ¥15 灰狼算法和蚁群算法如何结合
  • ¥15 这是一个利用ESP32自带按键和LED控制的录像代码,编译过程出现问题,请解决并且指出错误,指导如何处理 ,协助完成代码并上传代码
  • ¥20 stm32f103,hal库 hal_usart_receive函数接收不到数据。
  • ¥20 求结果和代码,sas利用OPTEX程序和D-efficiency生成正交集
  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?