Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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('<?xml ' . $xmlAttributes . "?>\n<" . $this->rootElementName . ">\n");
$stream->append('<?xml ' . trim($xmlAttributes) . "?>\n<" . $this->rootElementName . ">\n");
} else {
$stream = $streams->writeTo($this->path, $partitions);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ final class XMLParserExtractor implements Extractor, FileExtractor, LimitableExt
private string $xmlNodePath = '';

/**
* In order to iterate only over <element> nodes use `$loader->withXMLNodePath('root/elements/element')`.
* To iterate only over <element> nodes, use `$loader->withXMLNodePath('root/elements/element')`.
*
* <root>
* <elements>
Expand Down Expand Up @@ -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();

Expand All @@ -179,9 +146,12 @@ public function extract(FlowContext $context): Generator
return;
}
}

$this->elements = [];
}

xml_parse($this->parser(), '', true);

$this->freeParser();
}
}
Expand Down
Loading