3

http://www.bank.lv/vk/xml.xml?date=20130530

  $url = 'http://www.bank.lv/vk/xml.xml?date=20130530';
  $xml = simplexml_load_file($url) or die("feed not loading");
  $Rate = $xml->Currency[1]->Rate;
  echo $Rate;
  echo 'BREAK HTML';
  echo "-----";
  echo "// "; var_dump($xml); echo " //";

为什么 HTML 数据不输出?测试了很多教程,但不明白如何从这个 XML 输出数据

4

2 回答 2

3

你必须把

 $Rate = $xml->Currencies->Currency['1']->Rate;

代替

 $Rate = $xml->Currency[1]->Rate;

因为$xml结构是

   SimpleXMLElement Object
(
    [Date] => 20130530
    [Currencies] => SimpleXMLElement Object
    (
        [Currency] => Array
            (
                [0] => SimpleXMLElement Object
                    (
                        [ID] => AED
                        [Units] => 1
                        [Rate] => 0.14900000
                    )

                [1] => SimpleXMLElement Object
                    (
                        [ID] => AUD
                        [Units] => 1
                        [Rate] => 0.52300000
                    )

                [2] => SimpleXMLElement Object
                    (
                        [ID] => BGN
                        [Units] => 1
                        [Rate] => 0.35900000
                    )

                [3] => SimpleXMLElement Object
                    (
                        [ID] => BYR
                        [Units] => 1000
                        [Rate] => 0.06290000
                    )



                .
                .
                .

            )

    )

)
于 2013-05-30T06:39:12.753 回答
1

您的代码中缺少“货币”。它应该是:

$url = 'http://www.bank.lv/vk/xml.xml?date=20130530';
$xml = simplexml_load_file($url) or die("feed not loading");

$Rate = $xml->Currencies->Currency[1]->Rate;
echo $Rate;
于 2013-05-30T06:38:48.910 回答