php解析来自url的xml

时间:2013-05-30 06:29:10

标签: php xml xml-parsing

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输出数据

2 个答案:

答案 0 :(得分: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
                    )



                .
                .
                .

            )

    )

)

答案 1 :(得分:0)

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

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