从xml中的特定可识别标签之间进行刮擦

时间:2011-05-04 15:01:06

标签: php xml simplexml

我有以下提取货币名称货币代码国家和费率,问题是它抓取所有货币,我想只获得某些货币,即只有usd gbp和cad

<?php

$xchange = new SimpleXMLElement('http://www.bankisrael.gov.il/currency.xml', NULL, TRUE);

echo "
<table>
        <tr>
                <th>n</th>
                <th>cc</th>
                <th>c</th>
                <th>r</th>

        </tr>";

foreach($xchange as $curr) // loop through our books
{

        echo "
        <tr>
                <td>{$curr->NAME}</td>
                <td>{$curr->CURRENCYCODE}</td>
                <td>{$curr->COUNTRY}</td>
                <td>{$curr->RATE}</td>
        </tr>";
}

echo '</table>';
?>

6 个答案:

答案 0 :(得分:4)

这段代码应该以更清洁的方式完成工作:

<?php

$a = getCurrencyData('http://www.bankisrael.gov.il/currency.xml');
print_r($a);

function getCurrencyData($url) {

    $raw = file_get_contents($url);
    $xml = new SimpleXMLElement($raw);

    $ret = array();

    foreach($xml->CURRENCY as $currency) {
        $currency = (array) $currency;
        $ret[$currency['CURRENCYCODE']] = $currency;
    }

    return $ret;
}

您现在可以使用货币代码获取所需的货币,例如$a['GBP']

答案 1 :(得分:1)

这似乎足够合理:它过滤掉了$ filter中没有的所有东西。如果它在$ filter中,则会显示。我测试了它,它看起来像一个魅力。

<?php

/**
 * This is an array indicating which currencies you want to show.
 */
$filter = array(
    'usd',
    'gbp',
    'cad'
);

$root = simplexml_load_file( 'http://www.bankisrael.gov.il/currency.xml' );

echo "
<table>
    <tr>
        <th>n</th>
        <th>cc</th>
        <th>c</th>
        <th>r</th>
    </tr>\n";

foreach( $root->CURRENCY as $currency ) {
    if( in_array( (string) strtolower( $currency->CURRENCYCODE ), $filter ) ) {
        echo "
    <tr>
        <td>{$currency->NAME}</td>
        <td>{$currency->CURRENCYCODE}</td>
        <td>{$currency->COUNTRY}</td>
        <td>{$currency->RATE}</td>
    </tr>\n";
    }
}

echo "</table>\n";

答案 2 :(得分:0)

代码发言

$xchange = new SimpleXMLElement('http://www.bankisrael.gov.il/currency.xml', NULL, TRUE);
$filterCurrencies = array( 'USD', 'GBP' );

$filter = implode( array_map( function($filler) { return 'text()="'.$filler.'"'; }, $filterCurrencies), ' or ' );

$xpathQuery = $xpath = '//CURRENCYCODE[%filter%]/parent::*';
$xpathQuery = str_replace('%filter%', , $xpathQuery);

$currencies = $xchange->xpath($xpathQuery);

/** I do know you already have to code to echo it ... the code is tested, feel free to copy&pase **/

一步一步

好的,首先,您正在使用SimpleXML对象来读取以色列银行的数据。我建议利用这个对象完成大部分工作(与使用PHP过滤相比要快得多,尽管SimpleXML不是性能最好的。)

首先,我们想要完成什么? 根据元素的内容获取数据。 对于Webdesigner来说,这应该听起来像CSS,但不是很正确。对于一个Web开发人员,他的手上有一个听起来像XPath的XML,这是一个黄金选择! 幸运的是,SimpleXML使我们能够使用XPath,因此我们将构建一个查询:

XPath基础知识
//CURRENCYCODE将选择任何货币代码元素
//CURRENCYCODE/parent::*将选择货币代码parent(<CURRENCY>),这是我们的数据所在的位置 //CURRENCYCODE[text()="JPY"]只会选择文本等于JPY的<CURRENCY>个元素。

我们在这里讨论我们的需求清单:

$filterCurrencies = array( 'USD', 'GBP' ); // we want us dollars and british pounds
$filter =  implode( array_map( function($token) { return 'text()="'.$token.'"'; }, $filterCurrencies), ' or ' );
// this will make a string like 'text()="USD" or text()="GBP"' by mapping the filter against the requirements string (currenciecodes get tokens) glueing it with a logical or

现在唯一要做的就是将它与我​​们的XPATH模板集成

$xpath = '//CURRENCY[%filter%]/parent::*';
$xpath = str_replace('%filter%', $filter, $xpath);

$currencies = $xchange->xpath($xpath);

快乐循环!

答案 3 :(得分:0)

简单地替换

foreach($xchange as $curr) // loop through our books
{
        echo "
        <tr>
                <td>{$curr->NAME}</td>
                <td>{$curr->CURRENCYCODE}</td>
                <td>{$curr->COUNTRY}</td>
                <td>{$curr->RATE}</td>
        </tr>";
}

foreach($xchange as $curr) // loop through our books
{
    if (in_array($curr->CURRENCYCODE, array('usd', 'gbp', 'cad'))) {
            echo "
            <tr>
                    <td>{$curr->NAME}</td>
                    <td>{$curr->CURRENCYCODE}</td>
                    <td>{$curr->COUNTRY}</td>
                    <td>{$curr->RATE}</td>
            </tr>";
    }
}

答案 4 :(得分:0)

我这样做如下

function xml ($url) {
  $text = file_get_contents($url) or die("ERROR: Unable to read file");

  $p = xml_parser_create();

  xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
  xml_parse_into_struct($p, $text, $vals, $index);
  xml_parser_free($p);

  for($i=0; $i<count ($index['Title']); $i++) {
    foreach(array('var1', 'var2', 'var3') as $key) {
      $info[$i][$key] = $vals[$index[$key][$i]]['value'];
    }
  }
}

答案 5 :(得分:-1)

GOT IT

<?php

$xchange = new SimpleXMLElement('http://www.bankisrael.gov.il/currency.xml', NULL, TRUE);

echo "
<table>
        <tr>
                <th>n</th>
                <th>cc</th>
                <th>c</th>
                <th>r</th>

        </tr>

        <tr>
                <td>United States {$xchange->CURRENCY[0]->NAME} </td>
                <td>{$xchange->CURRENCY[0]->CURRENCYCODE} </td>
                <td>{$xchange->CURRENCY[0]->RATE} </td>
        </tr>
        <tr>
                <td>{$xchange->CURRENCY[1]->COUNTRY} {$xchange->CURRENCY[1]->NAME} </td>
                <td>{$xchange->CURRENCY[1]->CURRENCYCODE} </td>
                <td>{$xchange->CURRENCY[1]->RATE} </td>
        </tr>
        <tr>
                <td>{$xchange->CURRENCY[3]->NAME} </td>
                <td>{$xchange->CURRENCY[3]->CURRENCYCODE} </td>
                <td>{$xchange->CURRENCY[3]->RATE} </td>
        </tr>
        <tr>
                <td>{$xchange->CURRENCY[4]->COUNTRY}n {$xchange->CURRENCY[4]->NAME} </td>
                <td>{$xchange->CURRENCY[4]->CURRENCYCODE} </td>
                <td>{$xchange->CURRENCY[4]->RATE} </td>
        </tr>
        <tr>
                <td>Canadian {$xchange->CURRENCY[5]->NAME} </td>
                <td>{$xchange->CURRENCY[5]->CURRENCYCODE} </td>
                <td>{$xchange->CURRENCY[5]->RATE} </td>
        </tr>
        <tr>
                <td>{$xchange->CURRENCY[8]->COUNTRY}n {$xchange->CURRENCY[8]->NAME} </td>
                <td>{$xchange->CURRENCY[8]->CURRENCYCODE} </td>
                <td>{$xchange->CURRENCY[8]->RATE} </td>
        </tr>
        <tr>
                <td>Swiss {$xchange->CURRENCY[10]->NAME} </td>
                <td>{$xchange->CURRENCY[10]->CURRENCYCODE} </td>
                <td>{$xchange->CURRENCY[10]->RATE} </td>
        </tr>
</table>";


?>