编程语言
首页 > 编程语言> > PHP从URL解析XML

PHP从URL解析XML

作者:互联网

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

解决方法:

你必须把

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



                .
                .
                .

            )

    )

)

标签:xml,php,xml-parsing
来源: https://codeday.me/bug/20191123/2066014.html