user2435262
user2435262

Reputation: 33

php parse xml from url

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

Upvotes: 3

Views: 26303

Answers (2)

mohammad mohsenipur
mohammad mohsenipur

Reputation: 3149

you must put

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

instead of

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

because of $xml structure is

   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
                    )



                .
                .
                .

            )

    )

)

Upvotes: 3

Debashis
Debashis

Reputation: 596

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;

Upvotes: 1

Related Questions