simplexml_load_string没有解析正确

时间:2016-08-18 10:50:11

标签: php xml-parsing parsexml

所以,我试图解析和xml并从中获取一些值:

    $xml = simplexml_load_string(file_get_contents('http://www.bnr.ro/nbrfxrates.xml'));
    $currency = [];

    foreach($xml->Body->Cube->Rate as $rate)
    {
        $currency[] = [
            "name" => $rate["currency"],
            "value" => $rate,
            "multiplier" => $rate["multiplier"]
        ];
    }

    return $currency;

我的$ rate变量应该是Rate标签内的值(例如:1.0806)而不是它给我这个:

object(SimpleXMLElement)[110]
   public '@attributes' => 
      array (size=1)
         'currency' => string 'AED' (length=3)

1 个答案:

答案 0 :(得分:1)

将$ rate转换为(string)将起作用:

    $currency[] = [
        "name" => $rate["currency"],
        "value" => (string)$rate,
        "multiplier" => $rate["multiplier"]
    ];