简体   繁体   English

我如何通过simplexml_load_file从XML解析特定数据

[英]How can i parse specific data from XML via simplexml_load_file

I am facing the following XML: 我面临以下XML:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_PURCHASE_VALUTA
            [valid_from] => 2015-12-14 13:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 17.0266
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.326
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.4771
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 26.0743
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 19.5504
                        )

                )
        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_SALE_DEVIZA
            [valid_from] => 2015-12-14 13:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 18.1262
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.6161
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.7017
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 27.5739
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 20.813
                        )

                )
        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
            [valid_from] => 2015-12-14 13:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 17.34581
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.46043
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.54232
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 26.46609
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 19.91697
                        )

                )


        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_MIDDLE
            [valid_from] => 2015-12-14 13:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 17.736
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.53827
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.622
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 27.02
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 20.365
                        )

                )

        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_SALE_VALUTA
            [valid_from] => 2015-12-12 00:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 18.3578
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.6435
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.7488
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 27.8306
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 21.0322
                        )

                )
        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_PURCHASE_VALUTA
            [valid_from] => 2015-12-12 00:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 17.0275
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.3251
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.4771
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 26.0743
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 19.5082
                        )

                )
        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_SALE_DEVIZA
            [valid_from] => 2015-12-12 00:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 18.1272
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.6152
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.7017
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 27.5739
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 20.7681
                        )

                )
        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
            [valid_from] => 2015-12-12 00:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 17.34679
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.45955
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.54232
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 26.46609
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 19.87394
                        )

                )
        )

)
SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [type] => XML_RATE_TYPE_EBNK_MIDDLE
            [valid_from] => 2015-12-12 00:00:00.0
        )

    [currency] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => AUD
                            [quota] => 1
                            [rate] => 17.737
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => HRK
                            [quota] => 1
                            [rate] => 3.53737
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => DKK
                            [quota] => 1
                            [rate] => 3.622
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => EUR
                            [quota] => 1
                            [rate] => 27.02
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [name] => JPY
                            [quota] => 100
                            [rate] => 20.321
                        )

                )

        )

)

I am not fimilar with XML at all. 我根本不熟悉XML。 I have always used JSON. 我一直使用JSON。 I need to return 2 arrays from the XML. 我需要从XML返回2个数组。 the must listen to specific conditions. 必须听取具体情况。 I need only the [currency] array where the type is valid from today. 我只需要类型从今天开始有效的[currency]数组。 like 喜欢

[valid_from] => date('Y-m-d')

Online URL to the XML: XML的在线URL:

Link here 连结这里

I need two arrays one: 我需要两个数组之一:

@type:XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
@valid_from:2015-12-12 00:00:00.0

And

@type:XML_RATE_TYPE_EBNK_SALE_DEVIZA
@valid_from:2015-12-12 00:00:00.0

You can use instead DomDocument - http://php.net/manual/en/class.domdocument.php 您可以改用DomDocument - http: DomDocument

it allows you to read xml and select its nodes 它允许您读取xml并选择其节点

// Example
$dom = new DomDocument();
$dom->loadXML( file_get_contents($path) ); // $path -> where your xml saved

// it has methods get element by tag name       
$tag = $dom->getElementsByTagName("some_tag_name");// it will return array
// it has methods get element attributees
$tag->item(0)->getAttribute('some_attribute'); // you can take 0 index and read its attribute 
// to take tag value you can
$tag->item(0)->item(0)->nodeValue
// ...

If this basic example is not enough, if you can post your xml in your uestion i can try to help you with creation of exact commands 如果这个基本示例还不够,如果您可以将xml张贴到我的文章中,我可以尝试帮助您创建确切的命令

UPDATE UPDATE

$currencyArr1 = array();
$currencyArr2 = array();
$dom = new DomDocument();
$dom->loadXML( file_get_contents('http://104.236.115.118/application/currency_values/RB_Exchange_list.xml') );//$path path to your xml

$exchangeRates = $dom->getElementsByTagName("exchange_rate");

foreach ($exchangeRates as $exchangeRate){

    $validFrom = $exchangeRate->getAttribute('valid_from');// Your date
    $type = $exchangeRate->getAttribute('type');// Your type

    //First array case
    //@type:XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA
    //@valid_from:2015-12-12 00:00:00.0
    if( $type == "XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA" && 
        date('Y-m-d H:i:s', strtotime($validFrom)) == date('Y-m-d H:i:s', strtotime("2015-12-12 00:00:00")) ){

        $currencyes = $exchangeRate->getElementsByTagName("currency");

        foreach ($currencyes as $currency){
            $currencyArr1[] = array(
                    "name" => $currency->getAttribute("name"),
                    "quota" => $currency->getAttribute("quota"),
                    "rate" => $currency->getAttribute("rate"),
            );
        }
    }

    //Second array case
    //@type:XML_RATE_TYPE_EBNK_SALE_DEVIZA
    //@valid_from:2015-12-12 00:00:00
    if( $type == "XML_RATE_TYPE_EBNK_SALE_DEVIZA" && 
        date('Y-m-d H:i:s', strtotime($validFrom)) == date('Y-m-d H:i:s', strtotime("2015-12-12 00:00:00")) ){

        $currencyes = $exchangeRate->getElementsByTagName("currency");

        foreach ($currencyes as $currency){
            $currencyArr2[] = array(
                    "name" => $currency->getAttribute("name"),
                    "quota" => $currency->getAttribute("quota"),
                    "rate" => $currency->getAttribute("rate"),
            );
        }
    }
}

echo "ARRAY1:"; var_dump($currencyArr1);
echo "<br /><br />ARRAY2:"; var_dump($currencyArr2);

If you're interested in using SimpleXML, you should use the xpath function . 如果您对使用SimpleXML感兴趣,则应使用xpath函数

For example, this should theoretically let you iterate through all of the elements named "exchange_rate" with type=XML_RATE_TYPE_EBNK_SALE_VALUTA 例如,从理论上讲,这应该使您可以遍历type=XML_RATE_TYPE_EBNK_SALE_VALUTA所有名为“ exchange_rate”的元素

$content->xpath('exchange_rate[type="XML_RATE_TYPE_EBNK_SALE_VALUTA"]');

You can then loop through the result and compare the values of different attributes (found through the attribute property ). 然后,您可以遍历结果并比较不同属性的值(可通过attribute属性找到)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM