【问题标题】:How can i parse specific data from XML via simplexml_load_file我如何通过 simplexml_load_file 从 XML 解析特定数据
【发布时间】:2015-12-14 14:52:13
【问题描述】:

我面临以下 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
                        )

                )

        )

)

我根本不熟悉 XML。我一直使用 JSON。我需要从 XML 返回 2 个数组。一定要听具体情况。 我只需要类型从今天开始有效的[currency] 数组。喜欢

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

XML 的在线 URL:

Link here

我需要两个数组之一:

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

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

【问题讨论】:

  • 有点像 hack,但并不总是最好的主意,但如果您对 JSON(或本机数组)更满意,您可以使用 $json = json_encode(simplexml_load_file($path)); 将其转换为本机数组,然后您可以 @ 987654328@它。

标签: php xml codeigniter


【解决方案1】:

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

它允许你读取 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
// ...

如果这个基本示例还不够,如果您可以在您的 uestion 中发布您的 xml,我可以尝试帮助您创建确切的命令

更新

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

【讨论】:

  • 我已经添加了一个指向 XML 的链接,所以你可以看到它。和我需要的条件。希望你能帮忙!谢谢
  • 感谢您的帮助。我得到:数组(0){}
  • 我做了一些更新,我很着急,没有测试它现在应该可以工作的最后一部分,但请确保您的 xml 记录中有 valid_from="2015-12-14 00:00:00.0"type="XML_RATE_TYPE_EBNK_PURCHASE_DEVIZA" 根据您的日期有问题的条件
  • 仍然是空数组。我还修复了 $type == "XML_RATE_TYPE_EBNK_SALE_VALUTA" && 它只是一个等号。不知道发生了什么
  • 好的,再次尝试复制过去我修复的确切代码 $type == 和其他 attribute 选择部分,确保您的 xml 中有这样的标签 &lt;exchange_rate type="XML_RATE_TYPE_EBNK_SALE_VALUTA" valid_from="2015-12-14 00:00:00.0"&gt;
【解决方案2】:

如果您对使用 SimpleXML 感兴趣,您应该使用xpath function

例如,理论上这应该让您使用type=XML_RATE_TYPE_EBNK_SALE_VALUTA 遍历所有名为“exchange_rate”的元素

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

然后您可以遍历结果并比较不同属性的值(通过attribute property 找到)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    相关资源
    最近更新 更多