From f15331fb3f1f1d679b459fcb33e44fbf3657e295 Mon Sep 17 00:00:00 2001 From: Joseph Bielawski Date: Sat, 11 Jul 2026 12:49:07 +0200 Subject: [PATCH] Reduce complexity of XML reading & writing --- .../Flow/ETL/Adapter/XML/Loader/XMLLoader.php | 15 ++---- .../ETL/Adapter/XML/XMLParserExtractor.php | 54 +++++-------------- 2 files changed, 17 insertions(+), 52 deletions(-) diff --git a/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/Loader/XMLLoader.php b/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/Loader/XMLLoader.php index b6e895a00b..74d441432b 100644 --- a/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/Loader/XMLLoader.php +++ b/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/Loader/XMLLoader.php @@ -22,11 +22,7 @@ use Throwable; use function array_key_exists; -use function array_keys; -use function array_map; -use function array_values; use function count; -use function implode; final class XMLLoader implements Closure, FileLoader, Loader { @@ -194,13 +190,12 @@ public function write(Rows $nextRows, array $partitions, FlowContext $context, R $this->writes[$stream->path()->path()] = 0; } - $xmlAttributes = implode(' ', array_map( - static fn(string $key, string $value) => $key . '="' . $value . '"', - array_keys($this->xmlAttributes), - array_values($this->xmlAttributes), - )); + $xmlAttributes = ''; + foreach ($this->xmlAttributes as $key => $value) { + $xmlAttributes .= $key . '="' . $value . '" '; + } - $stream->append('\n<" . $this->rootElementName . ">\n"); + $stream->append('\n<" . $this->rootElementName . ">\n"); } else { $stream = $streams->writeTo($this->path, $partitions); } diff --git a/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/XMLParserExtractor.php b/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/XMLParserExtractor.php index 61d02d07cf..ebade62727 100644 --- a/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/XMLParserExtractor.php +++ b/src/adapter/etl-adapter-xml/src/Flow/ETL/Adapter/XML/XMLParserExtractor.php @@ -60,7 +60,7 @@ final class XMLParserExtractor implements Extractor, FileExtractor, LimitableExt private string $xmlNodePath = ''; /** - * In order to iterate only over nodes use `$loader->withXMLNodePath('root/elements/element')`. + * To iterate only over nodes, use `$loader->withXMLNodePath('root/elements/element')`. * * * @@ -124,51 +124,18 @@ public function extract(FlowContext $context): Generator )); } - if (count($this->elements)) { - foreach ($this->elements as $element) { - if ($shouldPutInputIntoRows) { - $rowData = [ - 'node' => $this->createDOMNode($element), - '_input_file_uri' => $uri, - ]; - } else { - $rowData = ['node' => $this->createDOMNode($element)]; - } - - $signal = yield array_to_rows( - $rowData, - $context->entryFactory(), - $stream->path()->partitions(), - $this->schema, - ); - - $this->incrementReturnedRows(); - - if ($signal === Signal::STOP || $this->reachedLimit()) { - $context->streams()->closeStreams($this->path); - $this->freeParser(); - - return; - } - } - $this->elements = []; - } - } - - xml_parse($this->parser(), '', true); - - if (count($this->elements)) { foreach ($this->elements as $element) { + $rowData = ['node' => $this->createDOMNode($element)]; if ($shouldPutInputIntoRows) { - $rowData = [ - 'node' => $this->createDOMNode($element), - '_input_file_uri' => $uri, - ]; - } else { - $rowData = ['node' => $this->createDOMNode($element)]; + $rowData['_input_file_uri'] = $uri; } - $signal = yield array_to_rows([$rowData], $context->entryFactory(), $stream->path()->partitions()); + $signal = yield array_to_rows( + $rowData, + $context->entryFactory(), + $stream->path()->partitions(), + $this->schema, + ); $this->incrementReturnedRows(); @@ -179,9 +146,12 @@ public function extract(FlowContext $context): Generator return; } } + $this->elements = []; } + xml_parse($this->parser(), '', true); + $this->freeParser(); } }