V
V
Vladimir2019-03-07 17:12:59
PHP
Vladimir, 2019-03-07 17:12:59

How to get the value of a tag from XML knowing the value of its attribute?

Good day everyone!
There is XML

<offer available="false" id="6014">
    <outlets>
      <outlet id="16" instock="26"/>
      <outlet id="36" instock="0"/>
    </outlets>
    <url/>
    <price>2062.00</price>
    <rrc_prcie>0.00</rrc_prcie>
    <categoryId>31</categoryId>
    <currencyId>RUB</currencyId>
    <name>CP 155/70R13 75T Cordiant Winter Drive PW-1</name>
    <model></model>
    <vendorCode>122973</vendorCode>
    <vendor/>
    <description>Описание отсутствует.</description>
    <param name="Шипы">Нешипованная</param>
    <param name="Назначение">легковая</param>
    <param name="Runflat">Нет</param>
    <param name="Типоразмер">155/70</param>
    <param name="Размер">R13</param>
    <param name="Конструкция шины">Радиальная</param>
    <param name="Камерность">Бескамерная</param>
    <param name="Шипы">Нешипованная</param>
    <param name="Скорость">190</param>
    <param name="Нагрузка на одинарную шину">387</param>
    <param name="Сезон">ЗИМНЯЯ</param>
    <param name="Не выгружать">Нет</param>
    <param name="Не включать в прайс">Нет</param>
    <param name="Модель шины">Winter Drive PW-1</param>
    <param name="Страна">отечественная</param>
    <param name="Объем коэффициент">1,1</param>
    <param name="Производитель">Cordiant</param>
    <param name="Код производителя">448496981</param>
    <param name="Ширина шины">155</param>
    <param name="Высота шины">70</param>
    <param name="Посадочный диаметр шины">13</param>
    <param name="Сезонность">Зимняя</param>
    <param name="Индекс скорости">T</param>
    <param name="Индекс нагрузки">387</param>
    <param name="Модель">Winter Drive PW-1</param>
    <param name="Марка (бренд)">Cordiant</param>
</offer>';

How to get the Winter Drive PW-1 value from the param tag knowing the name(Model) without using regular expressions, using SimpleXMLElement or similar functions that decompose XML into an object
I wrote such a function
function get_param ($xml_str, $param_name = '') 
{
  $xml = new SimpleXMLElement($xml_str);
  foreach ($xml as $key => $param) {
    if ($param['name'] == $param_name) {
      return (string)$param;
    }
  }
  return 'нет такого name';
}

But it is not very universal and somehow complicated, it seems to me there is an easier way

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
DevMan, 2019-03-07
L

use xpath Luke
https://3v4l.org/DG2Kk

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question