xml foreach函数查询

时间:2017-03-21 10:57:54

标签: php xml xml-parsing

我无法做两天,xml文件代码如下。

<?php 

        $tcmb = simplexml_load_file('http://www.tcmb.gov.tr/kurlar/today.xml'); 
        $euro = $tcmb->Currency[3]->BanknoteSelling; 
        $dolar = $tcmb->Currency[0]->BanknoteSelling; 

        $xml_data = simplexml_load_file('/public_html/xml/feed_4.xml'); 
        $degisim = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?><DATA></DATA>'); 

        foreach ($xml_data->RECORD as $fiyat) {

            $fiyat = $xml_data->RECORD->FIYAT; 
            $kur = $xml_data->RECORD->DOVIZ_BIRIMI; 
            $track = $degisim->addChild('RECORD'); 

            if ($kur == "USD") $fiyat = round (($xml_data->RECORD->FIYAT * (string)$dolar),2) ; 
            else if ($kur == "EUR") $fiyat = round (($xml_data->RECORD->FIYAT * (string)$euro),2); 
            else $fiyat = $xml_data->RECORD->FIYAT; 
            $track->addChild('STOK_KODU', $xml_data->RECORD->STOK_KODU);
            $track->addChild('newprice', $fiyat); 
            } 
        Header('Content-type: text/xml'); 
        print($degisim->asXML()); 
     ?>

本地XML文件,如

<?xml version="1.0"?>
<DATA>
 <RECORD>
  <STOK_KODU>PRODUCT001</STOK_KODU>
  <DOVIZ_BIRIMI>EUR</DOVIZ_BIRIMI>
  <FIYAT>26,5</FIYAT>
 </RECORD>
 <RECORD>
  <STOK_KODU>PRODUCT002</STOK_KODU>
  <DOVIZ_BIRIMI>USD</DOVIZ_BIRIMI>
  <FIYAT>23,805</FIYAT>
 </RECORD>
 <RECORD>
  <STOK_KODU>PRODUCT003</STOK_KODU>
  <DOVIZ_BIRIMI>TL</DOVIZ_BIRIMI>
  <FIYAT>28</FIYAT>
 </RECORD>
 <RECORD>
  <STOK_KODU>PRODUCT004</STOK_KODU>
  <DOVIZ_BIRIMI>TL</DOVIZ_BIRIMI>
  <FIYAT>9,25</FIYAT>
 </RECORD>
 <RECORD>
  <STOK_KODU>PRODUCT005</STOK_KODU>
  <DOVIZ_BIRIMI>TL</DOVIZ_BIRIMI>
  <FIYAT>9,25</FIYAT>
 </RECORD>
</DATA>

但是当像这样显示时会产生最佳产品

 <DATA>
    <RECORD>
    <STOK_KODU>PRODUCT001</STOK_KODU>
    <newprice>101.8</newprice>
    </RECORD>
    <RECORD>
    <STOK_KODU>PRODUCT001</STOK_KODU>
    <newprice>101.8</newprice>
    </RECORD>
    <RECORD>
    <STOK_KODU>PRODUCT001</STOK_KODU>
    <newprice>101.8</newprice>
    </RECORD>
    <RECORD>
    <STOK_KODU>PRODUCT001</STOK_KODU>
    <newprice>101.8</newprice>
    </RECORD>
    <RECORD>
    <STOK_KODU>PRODUCT001</STOK_KODU>
    <newprice>101.8</newprice>
    </RECORD>
    </DATA>

我希望像这样显示

<DATA>
<RECORD>
<STOK_KODU>PRODUCT001</STOK_KODU>
<newprice>101.8</newprice>
</RECORD>
<RECORD>
<STOK_KODU>PRODUCT002</STOK_KODU>
<newprice>93.2</newprice>
</RECORD>
<RECORD>
<STOK_KODU>PRODUCT003</STOK_KODU>
<newprice>28</newprice>
</RECORD>
<RECORD>
<STOK_KODU>PRODUCT004</STOK_KODU>
<newprice>9.25</newprice>
</RECORD>
<RECORD>
<STOK_KODU>PRODUCT005</STOK_KODU>
<newprice>9.25</newprice>
</RECORD>
</DATA>

我在哪里做错了? 我改变了很多代码,但我仍然不能

1 个答案:

答案 0 :(得分:0)

foreach循环中,您正在使用$xml_data->RECORD->...而不是当前元素的值。因此,您反复访问文档中的第一个 RECORD元素。

循环应该写成:

foreach ($xml_data->RECORD as $record) {
    $fiyat = floatval(str_replace(',', '.', $record->FIYAT));

    $factor = 1;
    $kur = $record->DOVIZ_BIRIMI;
    if ($kur == "USD") {
        $factor = $dolar;
    }
    else if ($kur == "EUR") {
        $factor = $euro;
    }

    $track = $degisim->addChild('RECORD');
    $track->addChild('STOK_KODU', $record->STOK_KODU);
    $track->addChild('newprice', round($fiyat * $factor, 2));
}

我还建议使用此功能,以使汇率检索更可靠。使用数组索引方法,如果货币的顺序发生变化,您将获得虚假结果。

function get_banknote_selling($xml, $currency) {
    return floatval($xml->xpath(sprintf('//Tarih_Date/Currency[@Kod="%s"]',
        $currency))[0]->BanknoteSelling);
}

$tcmb = simplexml_load_file('http://www.tcmb.gov.tr/kurlar/today.xml');
$euro = get_banknote_selling($tcmb, "EUR");
$dolar = get_banknote_selling($tcmb, "USD");