R
R
Roman Ilyin2019-11-11 16:34:57
PHP
Roman Ilyin, 2019-11-11 16:34:57

How to read attribute of child element in XMLReader?

There is a very large XML that is read sequentially using an XMLReader

<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://webmaster.yandex.ru/schemas/feed/realty/2010-06">
  <generation-date>2019-11-10T15:00:19+03:00</generation-date>
  <offer internal-id="14">
    <image>plan.jpg</image>
    <image>1.jpg</image>
    <image tag="plan">2.jpg</image>
    <image>3.jpg</image>
    <image>4.jpg</image>
  </offer>
  <offer>
    …
  </offer>
</feed>

I need to iterate through all offerand select only image tag="plan" among other attributes.
In the current implementation, I can only get the attribute of the offer element:
$reader = new XMLReader();
$reader->open('test.xml');
$doc = new DOMDocument;

while ($reader->read() && $reader->name !== 'offer');
while ($reader->name === 'offer')
{
    $node = simplexml_import_dom($doc->importNode($reader->expand(), true));
    print_r ($node->attributes()); // internal-id] => 14
    $reader->next('offer');
}

How to get attribute of image elements?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Bro, 2019-11-11
@GeForester

https://www.php.net/manual/ru/book.xmlreader.php

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question