Answer the question
In order to leave comments, you need to log in
How to get data from xml?
There is an xml file:
<CurrencyRates Name="Daily Exchange Rates" Date="13.01.2016">
<Currency ISOCode="USD">
<Nominal>1</Nominal>
<Value>75,8985</Value>
</Currency>
<Currency ISOCode="EUR">
<Nominal>1</Nominal>
<Value>82,6117</Value>
</Currency>
</CurrencyRates>
<?php
header('Content-type: text/html; charset=utf-8');
$xml = simplexml_load_file('xml/daily.xml');
echo $xml->CurrencyRates[Date];
foreach ($xml->Currency as $Currency) {
$number = $Currency->Value;
echo '<span class="cur-code">'.$Currency[ISOCode].'</span>';
echo '<span class="cur-val">'.substr("$number", 0, -2).'</span>'; /это потому-что я не смог преобразовать данные в число, решил просто обрезать два последних символа
}
Answer the question
In order to leave comments, you need to log in
<?php
$xml = <<<XML
<CurrencyRates Name="Daily Exchange Rates" Date="13.01.2016">
<Currency ISOCode="USD">
<Nominal>1</Nominal>
<Value>75,8985</Value>
</Currency>
<Currency ISOCode="EUR">
<Nominal>1</Nominal>
<Value>82,6117</Value>
</Currency>
</CurrencyRates>
XML;
$xml = simplexml_load_string($xml);
echo $xml['Date'];
foreach ($xml->Currency as $Currency) {
$number = $Currency->Value;
echo '<span class="cur-code">' . $Currency['ISOCode'] . '</span>';
echo '<span class="cur-val">' . sprintf('%.02f', str_replace(',', '.', $number)) . '</span>';
}
Используй SimpleXMLElement::attributes$xml->Currency->CurrencyRates->attributes('Date');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question