D
D
Darth Vader2015-11-14 10:55:56
PHP
Darth Vader, 2015-11-14 10:55:56

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>

I convert it to php array, looks like this
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, '.', ' ');
    }

Then we get the following array
Array
(
    [ErrorCode] =&gt; Ok
    [SubErrorCode] =&gt; Array
        (
        )
    [Result] =&gt; Array
        (
            [Content] =&gt; Array
                (
                    [Item] =&gt; Array
                        (
                            [0] =&gt; 67.966986
                            [1] =&gt; 0.160033
                            [2] =&gt; 49.347697
                            [3] =&gt; 314.526551
                            [4] =&gt; 0.093776
                            [5] =&gt; 49.347697
                            [6] =&gt; 314.526551
                            [7] =&gt; 0.093776
                        )
                )
        )
)

Here I am interested in the exchange rate
<Item FirstCode="USD" SecondCode="KZT">314.526551</Item>

in our php array it turns out
echo number_format ($array[Result][Content][Item][3], 0, '.', ' ');

but here's the problem, every time you update the exchange rate, the array changes and the location of the currencies in it too, for example, if now the USD to KZT rate is in 3rd place, then after the update it can become 5 or 1, and therefore it’s not possible to pick up right.
echo number_format ($array[Result][Content][Item][3], 0, '.', ' ');

Is it possible somehow to take only the USD to KZT rate from XML, because we already have values ​​for determining the currency, namely FirstCode="USD" SecondCode="KZT" , but I don't know how to do this, who can help?
I can't google, I live in China...

Answer the question

In order to leave comments, you need to log in

4 answer(s)
E
Eugene, 2015-11-14
@blackdarthvader

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;
        }
    }
}

Here in the IF of the construction, there is a check, if the Item's SecondCode parameter is equal to KZT and the same ITEM's FirstCode parameter is equal to USD, then we will display this ITEM - but since you have two identical Items, in your case it will display both. But my decision would be all the same. at what position in the array is your Item

V
Vitaliy Orlov, 2015-11-14
@orlov0562

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

X
xmoonlight, 2015-11-14
@xmoonlight

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!

A
Alexander Taratin, 2015-11-14
@Taraflex

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());

Under the hood is the same xpath, the overhead is not big. Especially since you don't have much data.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question