Skip to content

Large XML for multiple files #19

@evgeniyefimchenko

Description

@evgeniyefimchenko

Hello! Using the read-write-simplexml.php example
I need to split a very large file into several small ones, with a certain number of .
And no matter what I do, subsequent files are obtained with a broken structure. Here is an example code:
$iterationGet = $_GET['iterationGet'] ? $_GET['iterationGet'] : 1;
$itemsCount = 0;
$itemCount = 0;
$countIteration = 5000; // count <offer>
$xmlInputFile = 'my.xml';
$xmlOutputFile = 'newXml_' . $iterationGet . '.xml';
$reader = new XMLReader();
$reader->open($xmlInputFile);
$writer = new XMLWriter();
$writer->openUri($xmlOutputFile);
$iterator = new XMLWritingIteration($writer, $reader);
$writer->startDocument();
foreach ($iterator as $node) {
$isElement = $node->nodeType === XMLReader::ELEMENT;
if ($isElement && strtolower($node->name) === 'offers') {
$itemsCount++;
$itemCount = 0;
}
if ($isElement && strtolower($node->name) === 'offer') {
$itemCount++;
if ($itemCount == $iterationGet) {
$iterationGet++;
if ($iterationGet % $countIteration == 0) {
header('Refresh:2; url=index.php?iterationGet=' . $iterationGet);
break;
}
$writer->startElement($node->name);
$writer->writeAttribute('id', $itemsCount . "-" . $itemCount);
if ($node->isEmptyElement) {
$writer->endElement();
}
}
} else {
$iterator->write();
}
}
$writer->endDocument();

I would be grateful if you tell me how to implement this.
Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions