Can you please tell how to read and display data from an XML file? There is a file of the standard yandex

<yml_catalog date='******'> <shop> <categories> <category id='34'>Аккумуляторы</category> ....... </categories> <offers> <offer id='1' type='vendor.model' available='false'> .... <name>Аккумулятор 1</name> .... <param name='НапряТСниС' unit='Π’'>12.00</param> <param name='ΠΠΌΠΊΠΎΡΡ‚ΡŒ' unit='Ач'>2.40</param> <param name='Π’ΠΈΠΏ (тСхнология)'>Ni-Cd </param> <param name='ВрСмя зарядки' unit='ΠΌΠΈΠ½'>60.00</param> <param name='ВСс' unit='ΠΊΠ³'>0.32</param> <param name='ΠšΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚Π°Ρ†ΠΈΡ'></param> </offer> <offer id='2' type='vendor.model' available='false'> .... <name>Аккумулятор 2</name> .... <param name='НапряТСниС2' unit='Π’'>442.00</param> <param name='ΠΠΌΠΊΠΎΡΡ‚ΡŒ2' unit='Ач'>42.40</param> <param name='Π’ΠΈΠΏ (тСхнология)2'>Ni-Cd </param> <param name='ВрСмя зарядки2' unit='ΠΌΠΈΠ½'>260.00</param> <param name='ВСс2' unit='ΠΊΠ³'>20.32</param> <param name='ΠšΠΎΠΌΠΏΠ»Π΅ΠΊΡ‚Π°Ρ†ΠΈΡ2'>dsadsdd</param> </offer> </offers> </shop> 

I already have a read cycle for all data except param. How to arrange reading exactly param equal to each offer?

Did so

 foreach ($offers->shop->offers->offer[0]->param as $param) { $chname=$param['name']; $chunit=$param["unit"]; $chdigits=$param; } 

but it does not go through the cycle of the entire file, but only the first block of parameters (it repeats the same) and in the "offer id" settlers constantly displays ...

So:

  Battery1
   -param1
   -param1
   -param1
 Battery2
   -param1
   -param1
   -param1 

How to realize this, so that the identifier where id = 1, id = 2 and so on is transmitted from the top level? Or other ways to jump and display correctly .. But I did not find how to set a condition.

So that was:

  Battery1
   -param1
   -param1
   -param1
 Battery2
   -param2
   -param2
   -param2 
  • So you rewrite variables in the loop every time. - Naumov
  • So, how to make it read only all param in a cycle. If one value was, then it is clear .. but how can it be passed through one offer? these <param name = 'Voltage2' unit = 'B'> do not repeat, there may be various new names name - A. Richard

1 answer 1

All found the answer. The first cycle of values ​​I had was

 foreach ( $offers->shop->offers->offer as $offer ) : 

For param output just

 foreach ($offer->param as $param) { 

) and working. Thanks for Naumov for the comments)