Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions examples/child-nested-read.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

require('xmlreader-iterators.php'); // require XMLReaderIterator library

$xmlFile = 'data/products.xml';

/** @var \Traversable<int,\XMLReaderNode> $list */
$iterator = new XMLElementIterator(XMLReader::open($xmlFile));
$list = new XMLElementXpathFilter($iterator, '//product');

foreach ($list as $item) {
printf('Found product "%s"' . \PHP_EOL, $item->getAttribute('sku'));

foreach ($item->getChildElements('attributes') as $attributeList) {
foreach ($attributeList->getChildElements() as $attribute) {
printf(' - %s: %s' . \PHP_EOL, $attribute->name, (string)$attribute);
}
}
}
52 changes: 52 additions & 0 deletions examples/data/products.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="utf-8"?>
<catalog>
<product sku="empty_product"/>
<product sku="empty_attributes">
<attributes/>
</product>
<product sku="no_attributes">
</product>
<product sku="foo">
<attributes>
<name>Test product</name>
</attributes>
<variants>
<product sku="foo_blue">
<attributes>
<name>Test product in blue</name>
<price>15.00</price>
</attributes>
</product>
<product sku="foo_red">
<attributes>
<name>Test product in red</name>
<price>12.00</price>
</attributes>
</product>
</variants>
</product>
<product sku="bar">
<attributes>
<name>Simple test product</name>
<price>10.99</price>
</attributes>
</product>
<product sku="foobar">
<attributes>
<name>Another product</name>
<price>99.99</price>
</attributes>
<variants>
<product sku="foobar_l">
<attributes>
<size>L</size>
</attributes>
</product>
<product sku="foobar_m">
<attributes>
<size>M</size>
</attributes>
</product>
</variants>
</product>
</catalog>
39 changes: 37 additions & 2 deletions src/XMLChildElementIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ class XMLChildElementIterator extends XMLElementIterator
*/
private $name;

/**
* @var int
*/
private $innerDepth;

/**
* @inheritdoc
*
Expand Down Expand Up @@ -81,6 +86,11 @@ public function rewind()
!$this->moveToNextByNodeType(XMLReader::ELEMENT);
}

// handles e.g. <parent/> -> no children available
if ($this->reader->isEmptyElement) {
return;
}

if ($this->stopDepth === null) {
$this->stopDepth = $this->reader->depth;
}
Expand All @@ -89,6 +99,7 @@ public function rewind()
$result = $this->nextChildElementByName($this->name);

$this->index = $result ? 0 : null;
$this->innerDepth = 1;
$this->didRewind = true;
}

Expand Down Expand Up @@ -160,15 +171,15 @@ private function nextChildElementByName($name = null)
}
}

return (bool)$next;
return $next;
}

/**
* @return bool
*/
private function nextElement()
{
while ($this->reader->read()) {
while ($this->readNext()) {
if (XMLReader::ELEMENT !== $this->reader->nodeType) {
continue;
}
Expand All @@ -177,4 +188,28 @@ private function nextElement()
}
return false;
}

/**
* Wrap reading to track opening and closing tags to prevent reading not-children nodes
*
* @return bool
*/
protected function readNext()
{
// update inner depth
if ($this->reader->nodeType === XMLReader::ELEMENT && !$this->reader->isEmptyElement) {
$this->innerDepth++;
} elseif ($this->reader->nodeType === XMLReader::END_ELEMENT) {
$this->innerDepth--;

// all children read? Abort to prevent reading next node
if ($this->innerDepth === 0) {
// set pointer behind closing-tag
parent::readNext();
return false;
}
}

return parent::readNext();
}
}
8 changes: 7 additions & 1 deletion src/XMLReaderIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,17 @@ public function next()
if ($this->skipNextRead) {
$this->skipNextRead = false;
$this->lastRead = $this->reader->nodeType !== XMLReader::NONE;
} elseif ($this->lastRead = $this->reader->read() and $this->reader->nodeType === XMLReader::ELEMENT) {
} elseif ($this->lastRead = $this->readNext() and $this->reader->nodeType === XMLReader::ELEMENT) {
$this->touchElementStack();
}
}

#[\ReturnTypeWillChange]
protected function readNext()
{
return $this->reader->read();
}

/**
* @return string
* @since 0.0.19
Expand Down
21 changes: 21 additions & 0 deletions tests/functional/examples/Expectations/child-nested-read_php.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Found product "empty_product"
Found product "empty_attributes"
Found product "no_attributes"
Found product "foo"
- name: Test product
Found product "foo_blue"
- name: Test product in blue
- price: 15.00
Found product "foo_red"
- name: Test product in red
- price: 12.00
Found product "bar"
- name: Simple test product
- price: 10.99
Found product "foobar"
- name: Another product
- price: 99.99
Found product "foobar_l"
- size: L
Found product "foobar_m"
- size: M
23 changes: 22 additions & 1 deletion tests/unit/XMLChildElementIteratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ public function testIteration()
$this->assertEquals($expected[$index], $reader->name);
}
$this->assertEquals(count($expected), $count);

}

/**
Expand Down Expand Up @@ -142,4 +141,26 @@ public function testDescendantChildren()
$array = iterator_to_array($children, false);
$this->assertSame(array(), $array, 'all children have been consumed by foreach');
}

/**
* Test child-iterator without calling 'skipNextRead' method
*/
public function testIterationOnChildrenStopBeforeReadingNextElement()
{
$reader = XMLReader::open(__DIR__ . '/../../examples/data/sample-rss-091.xml');
$this->assertTrue(!!$reader, 'fixture document can be opened successfully');
$items = new XMLElementIterator($reader, 'item');
foreach ($items as $idx => $item) {
$children = $items->getChildElements();
$children->rewind();
foreach (array('title', 'link', 'description') as $tagName) {
$this->assertTrue($children->valid());
$this->assertSame($tagName, $children->name);
$children->next();
}
$this->assertFalse($children->valid());
}
$this->assertSame(6, $idx, 'sample-rss-091.xml element has 7 item elements');
$this->assertEmpty(\iterator_to_array($items), 'all children have been consumed by foreach');
}
}