Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't read large XML file from PHP

I have a huge XML file (114 KB/1719 lines; see the error message below why I say huge) and I try to read it as I have done with two similar files before.

The other two files load normally and are of comparable size the only difference being that the file in question contains Arabic text. So here is the PHP code:

$doc3 = new DOMDocument('1.0', 'utf-8');
$doc3->load($masternumber.'.xml');

And the error is:

Warning: DOMDocument::load() [domdocument.load]: Excessive depth in document: 256 use XML_PARSE_HUGE option in file: ...

Then $doc3 doesn't load the file. So I modified the code:

$doc3->load($masternumber.'.xml', "LIBXML_PARSEHUGE");

And I end-up with another warrning:

Warning: DOMDocument::load() expects parameter 2 to be long, string given in...

$doc3 is empty again.

What is wrong with it? The other files contain the same text in other languages and load properly but not this one? I am using PHP 5.3.9.

like image 336
RegedUser00x Avatar asked Jan 17 '23 15:01

RegedUser00x


1 Answers

Use a constant, not a string.

$doc3->load($masternumber.'.xml', LIBXML_PARSEHUGE);

See the DOMDocument::load() documentation for complete details. The second parameter is a long integer representing the selected options from the list of constants.

Incidentally, if you need multiple options for any reason, it is done by combining them with the bitwise OR operator |

// Multiple options  OR'd together...
// Just FYI, not specific to your situation...
$doc3->load($masternumber.'.xml', LIBXML_PARSEHUGE|LIBXML_NSCLEAN);
like image 196
Michael Berkowski Avatar answered Jan 28 '23 11:01

Michael Berkowski