netlogix/xml-processor

PHP XML-Processor based on XMLReader

1.1.0 2023-04-06 08:17 UTC

This package is auto-updated.

Last update: 2024-12-13 09:07:48 UTC


README

PHP XML-Processor based on XMLReader.

The XMLProcessor walks through the XML-file with the \XMLReader and fires events on each node of the \XMLReader. So its ease to process huge XML files with low memory usage.

Events

The following events are available:

How to use

To process an XML file, you need to create a nodeProcessor class. It has to implement the NodeProcessorInterface.

Where you can define NodeProcessorInterface::getSubscribedEvents on which events you want to react.

For easier use, you can extend the AbstractNodeProcessor class and implement one of the following interfaces:

Example

To extract all values of <value> nodes of the following XML:

file.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <value>foo</value>
    <value>bar</value>
    <value>baz</value>
</root>

Create a simple nodeProcessor class which collect all values of the <value> nodes.

OpenTestNodeProcessor.php

use Netlogix\XmlProcessor\NodeProcessor\AbstractNodeProcessor;
use Netlogix\XmlProcessor\NodeProcessor\OpenNodeProcessorInterface;
use Netlogix\XmlProcessor\NodeProcessor\Context\OpenContext;

class OpenValueNodeProcessor extends AbstractNodeProcessor implements OpenNodeProcessorInterface
{
    const NODE_PATH = 'value';
    private $nodeValues = [];

    public function openElement(OpenContext $context)
    {
        $xml = $context->getXmlProcessorContext()->getXmlReader();
        $node = $xml->expand();
        $this->nodeValues[] = $node->nodeValue;
    }

    function getNodeValues(): array
    {
        return $this->nodeValues;
    }
}

Create a new instance of the XmlProcessor class and attach the new nodeProcessor.

require_once 'OpenTestNodeProcessor.php';

require_once 'vendor/autoload.php';

$valueNodeProcessor = new OpenValueNodeProcessor();
$processor = new XMLProcessor([$valueNodeProcessor]);
$processor->processFile('file.xml');

var_dump($valueNodeProcessor->getNodeValues());

result:

array(3) {
  [0]=>
  string(3) "foo"
  [1]=>
  string(3) "bar"
  [2]=>
  string(3) "baz"
}