Skip to content
Thomas Weinert edited this page Jul 12, 2018 · 4 revisions

FluentDOM\XMLReader

FluentDOM\XMLReader extends XMLReader with some namespace support.

FluentDOM\XMLReader::read()

public function read(string $name = NULL, string $namespaceUri = NULL, callable $filter = NULL)

FluentDOM\XMLReader::read() now allows you to provide a tag name, a namespace URI and a filter function.

FluentDOM\XMLReader::next()

public function next(string $name = NULL, string $namespaceUri = NULL, callable $filter = NULL)

FluentDOM\XMLReader::next() had the $name parameter, already. It got namespace URI and a filter function added.

FluentDOM\XMLReader::registerNamespace()

public function registerNamespace(string $prefix, string $namespace)

This method registers a namespace on the XMLReader itself. This allows other methods to resolve prefixes in tag name arguments. The namespaces are propagated to FluentDOM\DOM\Document instances created by expand().

FluentDOM\XMLReader Iterators

FluentDOM now implements two iterators that abstract the XMLReader::read()/XMLReader::next() and XMLReader::expand(). The FluentDOM\XMLReader\Iterator iterates over all nodes that match the filters the value provided by the iterator is the expanded DOM node.

FluentDOM\SiblingIterator will iterate over the siblings of the first matched node. It will not go into child or parent nodes.

Example

The improvements may not look like much, but they considerably reduce the source needed for an implementation.

$reader = new FluentDOM\XMLReader();
$reader->open($file);
$reader->registerNamespace('s', 'http://www.sitemaps.org/schemas/sitemap/0.9');
$reader->registerNamespace('v', 'http://www.google.com/schemas/sitemap-video/1.1');

foreach (new FluentDOM\XMLReader\SiblingIterator($reader, 's:url') as $url) {
  var_dump(
    [
      $url('string(v:video/v:title)'),
      $url('string(s:loc)')
    ]
  );
}
Clone this wiki locally