How can I parse large XML files in PHP?

I am parsing an XML file about 12 MB in size. I need to analyze the whole file and save what I need to find in the MySQL database.

I turn an XML file into an array. Then I parse the array and save the values.

This works fine when the XML is very small, but it just stops behaving correctly when I run my 12 MB file.

I tried several functions that convert XML to an array that I found on the network, and none of them work.

This is a common error that I came across with two different XML array functions that I found on the Internet:

Fatal error: [] operator not supported for strings

I am using SimpleXML, is there a better way to resolve this? Are there any libraries other than SimpleXML that are powerful enough to handle large XML files?

I have it now:

$z = new XMLReader;
$z->open('feedfetch.xml');

$doc = new DOMDocument;
while ($z->read() && $z->name !== 'collection');
while ($z->name === 'collection')
{
    $node = simplexml_import_dom($doc->importNode($z->expand(), true));
    var_dump($node[0]);
    exit;

    $z->next('collection');
}

var? XML, , node ?

+3
2

SimpleXML XMLReader XML . Pull, .

+3

SimpleXML - , , , . , var_dump() SimpleXML; .

XML , SimpleXML, . $dom->element->subelement. , , . XML-, , . .

Unfortunatey, SimpleXML XML . , , , . , 12Mb , SimpleXML, , , .

0

Source: https://habr.com/ru/post/1775302/


All Articles