【问题标题】:How do i get attributes from this xml? (PHP)我如何从这个 xml 中获取属性? (PHP)
【发布时间】:2021-03-31 13:40:26
【问题描述】:

这里的学生,正在处理需要从页面获取信息的学校项目 用 php 卷曲。我现在有这个代码:

<?php

$curl = curl_init();
$url = "https://www.ecb.europa.eu/stats/eurofxref/eurofxref-daily.xml?5105e8233f9433cf70ac379d6ccc5775";
curl_setopt($curl,CURLOPT_URL,$url);
curl_setopt($curl,CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);


$data = curl_exec($curl);
curl_close($curl);

$xml = simplexml_load_string($data);

print("<pre>".print_r($xml,true)."</pre>");
?>

print 的输出是这样的: output

如何获取货币和汇率等属性?另外,我是否正确地进行了 xml 转换?我应该把它改成 json 吗?

编辑:输出文本

SimpleXMLElement Object
(
    [Cube] => SimpleXMLElement Object
        (
            [Cube] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [time] => 2021-03-31
                        )

                    [Cube] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => USD
                                            [rate] => 1.1725
                                        )

                                )

                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => JPY
                                            [rate] => 129.91
                                        )

                                )

                            [2] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [currency] => BGN
                                            [rate] => 1.9558
                                        )

                                )
                        )
                )

        )

)

【问题讨论】:

  • 检查 php 文档:php.net/manual/en/simplexmlelement.attributes.php,示例 #1。而且你不需要将它转换为json来获取数据
  • 小记 文本数据的图片没有帮助。将其发布为文本并格式化,然后可以对其进行索引,我们可以轻松地使用它来复制/粘贴到答案中,或者上帝禁止在发布之前实际测试答案

标签: php xml curl


【解决方案1】:

不幸的是,您正与命名空间纠缠在一起。尝试声明它们并使用 xpath:

$xml->registerXPathNamespace("xxx", "http://www.ecb.int/vocabulary/2002-08-01/eurofxref");
$currencies = $xml->xpath("//xxx:Cube[@currency]");
foreach($currencies as $currency) {
   $cur = $currency->xpath("./@currency")[0];
   $rate = $currency->xpath("./@rate")[0];   
   echo  $cur ." " . $rate . "\r\n" ;
   }

输出:

USD 1.1725
JPY 129.91
BGN 1.9558

等等

【讨论】:

    【解决方案2】:

    您可以获取 SimpleXML 文档的元素,就像您的情况一样,如下所示:

    $elements = $xml->Cube->Cube->Cube;
    

    这源于 XML 的结构,如下所示:

    <Cube>
    <Cube time="2021-03-31">
    <Cube currency="USD" rate="1.1725"/>
    [...]
    </Cube>
    </Cube>
    

    有了这个 ($elements),您可以像这样遍历列表中的所有货币:

    $xml = simplexml_load_string($data);
    $cubes = $xml->Cube->Cube->Cube;
    
    $rates = [];
    
    foreach ($cubes as $key => $cube) {
      $attrs = $cube->attributes();
    
      # We need to do typecasting since this still returns SimpleXML elements
      # but we want the string / numeric representation.
      $name = (string) $attrs['currency'];
      $rate = (float) $attrs['rate'];
      $rates[$name] = $rate;
    }
    
    var_dump($rates);
    
    // array(32) {
    //   ["USD"]=>
    //   float(1.1725)
    //   ["JPY"]=>
    //   float(129.91)
    //   ["BGN"]=>
    //   float(1.9558)
    //   ["CZK"]=>
    //   float(26.143)
    //   ["DKK"]=>
    //   float(7.4373)
    //   ["GBP"]=>
    //   float(0.85209)
    //   ["HUF"]=>
    //   float(363.27)
    //   ...
    // }
    

    最后,$rates 将是一个简单的数组,其中包含您的所有货币(它们的名称作为键)和它们的汇率。当然,您不需要将其保存到数组中,在foreach 循环中您也可以将其写入数据库。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      • 2012-10-22
      • 2013-11-21
      相关资源
      最近更新 更多