晚上好!
請有人指導我如何將具有相同價值的孩子添加到一個中。我有這個 XML 檔案:
我正在生成該 XML 檔案的當前代碼是:
<?php
$iso_curr_xml = simplexml_load_file("curr_old.xml");
$rates_json_url = file_get_contents("https://freecurrencyapi.net/api/v2/latest?apikey=4392e6c0-67d6-11ec-82cc-4d7a41b7625b&base_currency=GBP");
$rate_json_obj = json_decode($rates_json_url);
$ts = $rate_json_obj->query->timestamp;
$currencies = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><currencies ts="'.$ts.'" />');
foreach($iso_curr_xml as $xml_parse) {
foreach($xml_parse as $xml_res) {
$crcode = $xml_res->Ccy;
$currency = $currencies->addChild('currency');
$currency->addAttribute('code', $xml_res->Ccy);
$currency->addChild( 'ccode', $xml_res->Ccy );
$currency->addChild( 'cname', $xml_res->CcyNm );
$currency->addChild( 'cntry', $xml_res->CtryNm );
$currency->addChild( 'crate', $rate_json_obj->data->$crcode );
}
}
$currencies->asXML("currency.xml");
抱歉,我知道的最少是我認為我需要 xPath,但不知道如何。謝謝!
uj5u.com熱心網友回復:
在撰寫 XML 之前,您可以在陣列中重新組合(使用相同的鍵):
$iso_curr_xml = simplexml_load_file('https://wpcoder.co.uk//links/sir_prakash_webdev1/ws7/curr_old.xml');
$rates_json_url = file_get_contents('https://freecurrencyapi.net/api/v2/latest?apikey=4392e6c0-67d6-11ec-82cc-4d7a41b7625b&base_currency=GBP');
$rate_json_obj = json_decode($rates_json_url);
$ts = $rate_json_obj->query->timestamp;
// Regroup by crcode :
$codes = [];
foreach ($iso_curr_xml->CcyTbl->CcyNtry as $xml_res) {
$crcode = (string)$xml_res->Ccy;
// if the key doesn't exists, get shared data
if (!isset($codes[$crcode])) {
$codes[$crcode]['ccode'] = (string)$xml_res->Ccy;
$codes[$crcode]['cname'] = (string)$xml_res->CcyNm;
$codes[$crcode]['crate'] = $rate_json_obj->data->{$crcode} ?? 0;
}
// in all cases, add country name
$codes[$crcode]['cntry'][] = (string)$xml_res->CtryNm;
}
// Now, generate the XML (based on the previous array) :
$currencies = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><currencies ts="' . $ts . '" />');
foreach ($codes as $crcode => $data) {
$currency = $currencies->addChild('currency');
$currency->addAttribute('code', $crcode);
$currency->addChild('ccode', $data['ccode']);
$currency->addChild('cname', $data['cname']);
$currency->addChild('cntry', implode(', ', $data['cntry'])); // implode countries
$currency->addChild('cname', $data['crate']);
}
$currencies->asXML('currency.xml');
輸出:
<?xml version="1.0" encoding="UTF-8"?>
<currencies ts="1642451660">
<currency code="AFN">
<ccode>AFN</ccode>
<cname>Afghani</cname>
<cntry>AFGHANISTAN</cntry>
<cname>143.232437</cname>
</currency>
<currency code="EUR">
<ccode>EUR</ccode>
<cname>Euro</cname>
<cntry>?LAND ISLANDS, ANDORRA, AUSTRIA, BELGIUM, CYPRUS, ESTONIA, EUROPEAN UNION, FINLAND, FRANCE, FRENCH GUIANA, FRENCH SOUTHERN TERRITORIES (THE), GERMANY, GREECE, GUADELOUPE, HOLY SEE (THE), IRELAND, ITALY, LATVIA, LITHUANIA, LUXEMBOURG, MALTA, MARTINIQUE, MAYOTTE, MONACO, MONTENEGRO, NETHERLANDS (THE), PORTUGAL, RéUNION, SAINT BARTHéLEMY, SAINT MARTIN (FRENCH PART), SAINT PIERRE AND MIQUELON, SAN MARINO, SLOVAKIA, SLOVENIA, SPAIN</cntry>
<cname>1.198999</cname>
</currency>
<!-- ... -->
</currencies>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/415025.html
標籤:
