dongzhuo3059 2009-10-31 03:41
浏览 32
已采纳

需要一些XML解析方面的帮助

The XML feed is located at: http://xml.betclick.com/odds_fr.xml

I need a php loop to echo the name of the match, the hour, and the bets options and the odds links. The function will select and display ONLY the matchs of the day with streaming="1" and the bets type "Ftb_Mr3".

I'm new to xpath and simplexml.

Thanks in advance.

So far I have:

<?php
$xml_str = file_get_contents("http://xml.betclick.com/odds_fr.xml");
$xml = simplexml_load_string($xml_str);

// need xpath magic
$xml->xpath();

// display

?>
  • 写回答

3条回答 默认 最新

  • duangong937906 2009-10-31 04:42
    关注

    Xpath is pretty simple once you get the hang of it

    you basically want to get every match tag with a certain attribute

    //match[@streaming=1]
    

    will work pefectly, it gets every match tag from underneath the parent tag with the attribute streaming equal to 1

    And i just realised you also want matches with a bets type of "Ftb_Mr3"

    //match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]
    

    This will return the bet node though, we want the match, which we know is the grandparent

    //match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..
    

    the two dots work like they do in file paths, and gets the match.

    now to work this into your sample just change the final bit to

    // need xpath magic
    $nodes = $xml->xpath('//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..');
    
    foreach($nodes as $node) {
        echo $node['name'].'<br/>';
    }
    

    to print all the match names.

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

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100