php php从url解析xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16829375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:44:57  来源:igfitidea点击:

php parse xml from url

phpxmlxml-parsing

提问by user2435262

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

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 " //";

Why HTML data dont output? Had tested lot of tutorials, but dont get, how to output data from this XML

为什么HTML数据不输出?已经测试了很多教程,但没有得到,如何从这个 XML 输出数据

采纳答案by mohammad mohsenipur

you must put

你必须把

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

instead of

代替

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

because of $xmlstructure is

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



                .
                .
                .

            )

    )

)

回答by Debashis

You are missing 'Currencies' in your code. It should be:

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

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