Answer the question
In order to leave comments, you need to log in
How to get data from xml using php?
Hello.
The situation is this, there is an xml file that sends me exchange rates, it looks like this
<Result>
<Content>
<Item FirstCode="USD" SecondCode="RUB">67.966986</Item>
<Item FirstCode="CNY" SecondCode="USD">0.160033</Item>
<Item FirstCode="CNY" SecondCode="KZT">49.347697</Item>
<Item FirstCode="USD" SecondCode="KZT">314.526551</Item>
<Item FirstCode="RUB" SecondCode="CNY">0.093776</Item>
<Item FirstCode="CNY" SecondCode="KZT">49.347697</Item>
<Item FirstCode="USD" SecondCode="KZT">314.526551</Item>
<Item FirstCode="RUB" SecondCode="CNY">0.093776</Item>
</Content>
</Result>
public static function GetInstanceCurrencyRateList() {
$xmlstring=file_get_contents("ссылка на файл");
$xml = simplexml_load_string($xmlstring);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);
//echo number_format ($array[Result][Content][Item][3], 0, '.', ' ');
}
Array
(
[ErrorCode] => Ok
[SubErrorCode] => Array
(
)
[Result] => Array
(
[Content] => Array
(
[Item] => Array
(
[0] => 67.966986
[1] => 0.160033
[2] => 49.347697
[3] => 314.526551
[4] => 0.093776
[5] => 49.347697
[6] => 314.526551
[7] => 0.093776
)
)
)
)
<Item FirstCode="USD" SecondCode="KZT">314.526551</Item>
echo number_format ($array[Result][Content][Item][3], 0, '.', ' ');
echo number_format ($array[Result][Content][Item][3], 0, '.', ' ');
Answer the question
In order to leave comments, you need to log in
The only thing is that you have two identical lines.
They probably copied it wrong.
I can only offer a crooked code, but it will return what you need
$currency = simplexml_load_file("test.xml");
foreach($currency->Result->Content as $v){
$valuta = $v->Item;
foreach($valuta as $val){
if($val['SecondCode']=='KZT'&&$val['FirstCode']=='USD'){
echo $val;
}
}
}
I'm too lazy to write code, I'll tell you how to do it:
1) simplexml_load_string, returns an object of the SimpleXMLElement class:
php.net/manual/en/function.simplexml-load-string.php
2) The SimpleXMLElement class has an xpath method:
php. net/manual/en/simplexmlelement.xpath.php
3) In xpath, you can select by attributes:
www.w3schools.com/xsl/xpath_syntax.asp
There are examples on the links.
And don't listen to shitty coders of uneducated programmers who use preg_match to parse XML
Use: preg_match()
Vitaliy Orlov , I will explain for the educated:
1. In this case, the intermediate links do not contain elements, i.e. leaf container - flat.
2. If the structure of the XML document is incorrect - simplexml* will stop with an error and that's it.
3. It has a limit on the size of the XML document.
Therefore, it is wiser to use preg_match() here
PS: Alexey Ukolov , Thank you!
https://github.com/olamedia/nokogiri
Parsing will look something like this
$сontentElement= (new nokogiri(<xml строка>))->get('Content');
var_dump($сontentElement->get('Item[FirstCode="USD"]')->toText());
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question