XML获取多个值

时间:2013-07-18 19:52:08

标签: php xml

我有以下XML文件。不同网站不断重复4行。 这些是_URL _Away _Home _Draw。每个都在网站前面。我需要比较所有_Away行以找到最高值,但有时可能有1行,其他时间可能有多达32行。我想知道的是,有没有办法获得这些通过定义字符串的结尾而不必为每个网站显式声明整个字符串?

<XMLSOCCER.COM>
    <Odds>
        <Id>1547</Id>
        <_10Bet_Home_Home>1.31</_10Bet_Home_Home>
        <_10Bet_Home_Url>http://en.10bet.com</_10Bet_Home_Url>
        <_10Bet_Home_Away>8.50</_10Bet_Home_Away>
        <_10Bet_Home_Draw>5.40</_10Bet_Home_Draw>
        <Bet_At_Home_Home>1.25</Bet_At_Home_Home>
        <Bet_At_Home_Url>http://www.bet-at-home.com/</Bet_At_Home_Url>
        <Bet_At_Home_Away>10.00</Bet_At_Home_Away>
        <Bet_At_Home_Draw>5.75</Bet_At_Home_Draw>
        <Bet365_Url>http://www.bet365.com/</Bet365_Url>
        <Bet365_Home>1.30</Bet365_Home>
        <Bet365_Away>9.00</Bet365_Away>
        <Bet365_Draw>5.50</Bet365_Draw>
        <BetVictor_Home>1.30</BetVictor_Home>
        <BetVictor_Url>http://www.betvictor.com/</BetVictor_Url>
        <BetVictor_Away>9.00</BetVictor_Away>
        <BetVictor_Draw>5.40</BetVictor_Draw>
        <Bwin_Home>1.28</Bwin_Home>
    </Odds>
</XMLSOCCER.COM>

3 个答案:

答案 0 :(得分:1)

您可以使用XPath获取以_Away结尾的所有节点。 Here's a code snippet完成你想要的东西:

<?php
$xml = <<<XML
<XMLSOCCER.COM>
<Odds>
    <Id>1547</Id>
    <_10Bet_Home_Home>1.31</_10Bet_Home_Home>
    <_10Bet_Home_Url>http://en.10bet.com</_10Bet_Home_Url>
    <_10Bet_Home_Away>8.50</_10Bet_Home_Away>
    <_10Bet_Home_Draw>5.40</_10Bet_Home_Draw>
    <Bet_At_Home_Home>1.25</Bet_At_Home_Home>
    <Bet_At_Home_Url>http://www.bet-at-home.com/</Bet_At_Home_Url>
    <Bet_At_Home_Away>10.00</Bet_At_Home_Away>
    <Bet_At_Home_Draw>5.75</Bet_At_Home_Draw>
    <Bet365_Url>http://www.bet365.com/</Bet365_Url>
    <Bet365_Home>1.30</Bet365_Home>
    <Bet365_Away>9.00</Bet365_Away>
    <Bet365_Draw>5.50</Bet365_Draw>
    <BetVictor_Home>1.30</BetVictor_Home>
    <BetVictor_Url>http://www.betvictor.com/</BetVictor_Url>
    <BetVictor_Away>9.00</BetVictor_Away>
    <BetVictor_Draw>5.40</BetVictor_Draw>
    <Bwin_Home>1.28</Bwin_Home>
</Odds>
</XMLSOCCER.COM>
XML;

$sxe                 = new SimpleXMLElement($xml);
$nodesEndingWithAway = $sxe->xpath('//*[substring(name(),string-length(name())-3) = "Away"]');

$highestValue = 0;
$nodeName     = '';

foreach ($nodesEndingWithAway as $node) {
    if ((float) $node > $highestValue) {
        $highestValue = (float) $node;
        $nodeName     = $node->getName();
    }
}

echo "Highest value is {$highestValue} from node {$nodeName}.\n";

输出:

Highest value is 10 from node Bet_At_Home_Away.

注意:我认为可以使用单个XPath表达式来完成它,而无需使用foreach处理节点。

答案 1 :(得分:0)

您可以使用XPath执行此操作。

$doc = new DOMDocument();
$doc->load($filename);

$xpath = new DOMXPath($doc);
$elements = $xpath->query('/XMLSOCCER.COM/Odds/*[substring(name(),string-length(name())-3) = "Away"]');

$maxValue = 0;
foreach ($elements as $element) {
  $value = floatval($element->nodeValue);
  $maxValue = max($maxValue, $value);
}  

答案 2 :(得分:0)

编辑:非常压缩:

$maxbid = max(array_map('floatval', $xml->xpath("//*[substring(name(),string-length(name())-" . (strlen($search) - 1) . ") = '$search']")));

分几个步骤:

使用simplexmlxpath

$search = "_Away";
$xml = simplexml_load_string($x);
$results = $xml->xpath("//*[substring(name(),string-length(name())-" . (strlen($search) - 1) . ") = '$search']");

循环搜索结果:

foreach ($results as $result) echo "$result <br />";

打印最高结果:

echo "highest: " . number_format(max(array_map('floatval', $results)), 2, '.', ',');

看到它正常工作:http://codepad.viper-7.com/iEpGz9