D
D
Denai2015-02-01 21:03:52
PHP
Denai, 2015-02-01 21:03:52

How to remove/get parameters from XML?

We take a file like this:

...
<offers>
<offer id="17" available="true">
<url>test.ru</url>
<name>тестовый элемент</name>
<param name="Вес">27</param>
<param name="Длина">16</param>
<param name="Хрупкий"/>
<param name="Синий">Да</param>
</offer>
<offer id="18" available="false">
<url>test2.ru</url>
<name>тестовый элемент2</name>
<param name="Вес">11</param>
<param name="Длина">89</param>
<param name="Жидкий"/>
<param name="Синий">Нет</param>
</offer>
.....

I am uploading a file:
$ourfile = simplexml_load_file($name, 'SimpleXMLElement', LIBXML_NOBLANKS);

In it I can refer to the element, for example, remove the link I don’t need
foreach ($ourfile->offers->offer as $offer) {unset($offer->url);}

Or display / replace the values ​​\u200b\u200bthat I need. But with "param" I'm doing something wrong.
$offer->param["name"] - displays the name of the parameter, I mastered it.
I need:
  • Remove parameter where length is specified
  • Output weight to variable
  • Find out if it's fragile

Actually I would like to know how to do it right.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
DevMan, 2015-02-02
@Denai

You need XPath .
Gaining weight:
Then I think you can do it yourself.

S
Sergey, 2019-05-16
@oldzas

foreach ($item->param as $param){
$param = (array)$param;
print_r($param["@attributes"]["name"]);
print_r($param[0]);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question