Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/259.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 需要XML解析方面的帮助吗_Php_Xml_Simplexml - Fatal编程技术网

Php 需要XML解析方面的帮助吗

Php 需要XML解析方面的帮助吗,php,xml,simplexml,Php,Xml,Simplexml,XML提要位于: 我需要一个php循环来回显比赛名称、时间、赌注选项和赔率链接。 该功能将仅选择并显示当天的比赛,并使用streaming=“1”和下注类型“Ftb_Mr3” 我不熟悉xpath和simplexml 提前谢谢 到目前为止,我已经: <?php $xml_str = file_get_contents("http://xml.betclick.com/odds_fr.xml"); $xml = simplexml_load_string($xml_str); // need

XML提要位于:

我需要一个php循环来回显比赛名称、时间、赌注选项和赔率链接。 该功能将仅选择并显示当天的比赛,并使用streaming=“1”和下注类型“Ftb_Mr3”

我不熟悉xpath和simplexml

提前谢谢

到目前为止,我已经:

<?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

?>

我真的不知道如何使用xpath,但如果您想“循环”,这应该让您开始:

<?php
$xml = simplexml_load_file("odds_fr.xml");

  foreach ($xml->children() as $child)
  {
    foreach ($child->children() as $child2)
    {
      foreach ($child2->children() as $child3)
      {
        foreach($child3->attributes() as $a => $b)
        {
          echo $a,'="',$b,"\"</br>";
        }

      }
    }
  }
?> 

这将使您找到具有“流”属性的“匹配”标记。我也不知道今天的比赛是什么,但是

它基本上与w3c参考文献完全一致:

我真的不知道如何使用xpath,但如果您想“循环”,这应该让您开始:

<?php
$xml = simplexml_load_file("odds_fr.xml");

  foreach ($xml->children() as $child)
  {
    foreach ($child->children() as $child2)
    {
      foreach ($child2->children() as $child3)
      {
        foreach($child3->attributes() as $a => $b)
        {
          echo $a,'="',$b,"\"</br>";
        }

      }
    }
  }
?> 

这将使您找到具有“流”属性的“匹配”标记。我也不知道今天的比赛是什么,但是

它基本上与w3c参考文献完全一致:
一旦掌握了诀窍,Xpath就非常简单了

您基本上希望获得具有特定属性的每个匹配标记

//match[@streaming=1]
将非常有效,它从父标记下面获取属性流等于1的每个匹配标记

我刚意识到你也想要“Ftb_Mr3”类型的下注匹配

这将返回bet节点,我们想要匹配,我们知道它是祖父母

//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..
这两个点的工作方式与文件路径中的相同,并获得匹配

现在要将其应用到示例中,只需将最后一位更改为

// need xpath magic
$nodes = $xml->xpath('//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..');

foreach($nodes as $node) {
    echo $node['name'].'<br/>';
}
//需要xpath魔法吗
$nodes=$xml->xpath('//match[@streaming=1]/bets/bett[@code=“Ftb_Mr3”]/../);
foreach($node作为$node){
echo$node['name']。
; }

打印所有匹配名称。

一旦掌握了诀窍,Xpath就非常简单了

您基本上希望获得具有特定属性的每个匹配标记

//match[@streaming=1]
将非常有效,它从父标记下面获取属性流等于1的每个匹配标记

我刚意识到你也想要“Ftb_Mr3”类型的下注匹配

这将返回bet节点,我们想要匹配,我们知道它是祖父母

//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..
这两个点的工作方式与文件路径中的相同,并获得匹配

现在要将其应用到示例中,只需将最后一位更改为

// need xpath magic
$nodes = $xml->xpath('//match[@streaming=1]/bets/bet[@code="Ftb_Mr3"]/../..');

foreach($nodes as $node) {
    echo $node['name'].'<br/>';
}
//需要xpath魔法吗
$nodes=$xml->xpath('//match[@streaming=1]/bets/bett[@code=“Ftb_Mr3”]/../);
foreach($node作为$node){
echo$node['name']。
; }

打印所有匹配的名称。

我在一个项目中使用它。刮削与以下方面的差异:

<?php
        $match_csv = fopen('matches.csv', 'w');
        $bet_csv = fopen('bets.csv', 'w');
        $xml = simplexml_load_file('http://xml.cdn.betclic.com/odds_en.xml');
        $bookmaker = 'Betclick';
        foreach ($xml as $sport) {
            $sport_name = $sport->attributes()->name;
            foreach ($sport as $event) {
                $event_name = $event->attributes()->name;
                foreach ($event as $match) {
                    $match_name = $match->attributes()->name;
                    $match_id = $match->attributes()->id;
                    $match_start_date_str = str_replace('T', ' ', $match->attributes()->start_date);
                    $match_start_date = strtotime($match_start_date_str);
                    if (!empty($match->attributes()->live_id)) {
                        $match_is_live = 1;
                    } else {
                        $match_is_live = 0;
                    }
                    if ($match->attributes()->streaming == 1) {
                        $match_is_running = 1;
                    } else {
                        $match_is_running = 0;
                    }
                    $match_row = $match_id . ',' . $bookmaker . ',' . $sport_name . ',' . $event_name . ',' . $match_name . ',' . $match_start_date . ',' . $match_is_live . ',' . $match_is_running;
                    fputcsv($match_csv, explode(',', $match_row));
                    foreach ($match as $bets) {
                        foreach ($bets as $bet) {
                            $bet_name = $bet->attributes()->name;
                            foreach ($bet as $choice) {
                                // team numbers are surrounded by %, we strip them
                                $choice_name = str_replace('%', '', $choice->attributes()->name);
                                // get the float value of odss
                                $odd = (float)$choice->attributes()->odd;
                                // concat the row to be put to csv file
                                $bet_row = $match_id . ',' . $bet_name . ',' . $choice_name . ',' . $odd;
                                fputcsv($bet_csv, explode(',', $bet_row));
                            }
                        }
                    }
                }
            }
        }
        fclose($match_csv);
        fclose($bet_csv);
?>


然后将csv文件加载到mysql中。每分钟运行一次,到目前为止效果很好。

我正在一个项目中使用它。刮削与以下方面的差异:

<?php
        $match_csv = fopen('matches.csv', 'w');
        $bet_csv = fopen('bets.csv', 'w');
        $xml = simplexml_load_file('http://xml.cdn.betclic.com/odds_en.xml');
        $bookmaker = 'Betclick';
        foreach ($xml as $sport) {
            $sport_name = $sport->attributes()->name;
            foreach ($sport as $event) {
                $event_name = $event->attributes()->name;
                foreach ($event as $match) {
                    $match_name = $match->attributes()->name;
                    $match_id = $match->attributes()->id;
                    $match_start_date_str = str_replace('T', ' ', $match->attributes()->start_date);
                    $match_start_date = strtotime($match_start_date_str);
                    if (!empty($match->attributes()->live_id)) {
                        $match_is_live = 1;
                    } else {
                        $match_is_live = 0;
                    }
                    if ($match->attributes()->streaming == 1) {
                        $match_is_running = 1;
                    } else {
                        $match_is_running = 0;
                    }
                    $match_row = $match_id . ',' . $bookmaker . ',' . $sport_name . ',' . $event_name . ',' . $match_name . ',' . $match_start_date . ',' . $match_is_live . ',' . $match_is_running;
                    fputcsv($match_csv, explode(',', $match_row));
                    foreach ($match as $bets) {
                        foreach ($bets as $bet) {
                            $bet_name = $bet->attributes()->name;
                            foreach ($bet as $choice) {
                                // team numbers are surrounded by %, we strip them
                                $choice_name = str_replace('%', '', $choice->attributes()->name);
                                // get the float value of odss
                                $odd = (float)$choice->attributes()->odd;
                                // concat the row to be put to csv file
                                $bet_row = $match_id . ',' . $bet_name . ',' . $choice_name . ',' . $odd;
                                fputcsv($bet_csv, explode(',', $bet_row));
                            }
                        }
                    }
                }
            }
        }
        fclose($match_csv);
        fclose($bet_csv);
?>


然后将csv文件加载到mysql中。每分钟运行一次,到目前为止效果很好。

到目前为止,您想出了什么主意?一旦我找到xpath,我就可以循环它了。到目前为止你想出了什么?一旦我弄明白xpath,我就可以循环它了。嘿,这很酷,而且很有道理。谢谢我喜欢w3schools,但对我来说,这些例子通常没有意义,直到我看到上面的海报和你的例子。这很奇怪,但这只是我学习的方式。有时会让我发疯,因为很多人认为如果你学习的方式与其他人不同,你就不是一个好的程序员。不管怎么说,我不想大声嚷嚷,但我有点诵读困难,所以对我来说,谈论一些事情作为开门红要容易得多。不知何故,在那之后,它就像一把钥匙打开了某些东西,我可以进入“标准”引用。XPath表达式应该是
//match[@streaming=“1”][bets/bet/@code=“Ftb_Mr3”]
——您正在寻找的节点是“match”,其余的是谓词,它们应该被视为是正确的,这很酷,而且很有意义。谢谢我喜欢w3schools,但对我来说,这些例子通常没有意义,直到我看到上面的海报和你的例子。这很奇怪,但这只是我学习的方式。有时会让我发疯,因为很多人认为如果你学习的方式与其他人不同,你就不是一个好的程序员。不管怎么说,我不想大声嚷嚷,但我有点诵读困难,所以对我来说,谈论一些事情作为开门红要容易得多。不知何故,在那之后,它就像一把钥匙打开了某些东西,我可以进入“标准”引用。XPath表达式应该是
//match[@streaming=“1”][bets/bet/@code=“Ftb_Mr3”]
——您要查找的节点是“match”,其余的是谓词,它们应该被视为suchRead我的另一个注释。我同意。请阅读我的其他评论。我同意。