I'm build a huge xml->mysql data importer ar parser from outer server.
And I need a script example, what to do with big
$xml = simplexml_load_file('plane.xml');
if($xml) {
for($lvl1=0; $lvl1<(count($xml->Trip)); $lvl1++) {
for($lvl3=0; $lvl3<(count($xml->Trip[$lvl1]->Hotels->Hotel)); $lvl3++) {
// Fill an array with the travel data
$element['trip_id'] = (string)$xml->Trip[$lvl1]['id'];
$element['description'] = (string)$xml->Trip[$lvl1]->Description;
$element['hotel_name'] = (string)$xml->Trip[$lvl1]->Hotels->Hotel[$lvl3]->HotelName;
$element['hotel_stars'] = (string)$xml->Trip[$lvl1]->Hotels->Hotel[$lvl3]->HotelStars;
}
All is working when I do a 1-3 MB XML import.
The problem is, what I must do 5-6 ~80-90 MB XML files import to sql. So, first of all - it makes website lagg when website trying to donwload the file, and overload server when it takes all the file I cache, as I understand?
The website from which I must Import XML's, also providers a xml.gz files, they all fits in ~2-3MB instead of ~80-90MB.
But I don't know how to rewrite my script, or need a help with example of 1 Object input from .gz file.
Php.net provides a .gz decompression scripts(for reading by size, or by every line as strings):
<?php
// get contents of a gz-file into a string
$filename = "/usr/local/something.txt.gz";
$zd = gzopen($filename, "r");
$contents = gzread($zd, 10000);
gzclose($zd);
?>
Also on IBM.com I had found an example of part reading:
// set up the request
$request = $HTTP_RAW_POST_DATA;
error_reporting(E_ERROR | E_WARNING | E_PARSE);
// create the reader
$reader = new XMLReader();
$reader->setRelaxNGSchema("request.rng");
$reader->XML($request);
$input = "";
while ($reader->read()) {
if ($reader->name == "double" && $reader->nodeType == XMLReader::ELEMENT) {
while ($reader->read()) {
if ($reader->nodeType == XMLReader::TEXT
|| $reader->nodeType == XMLReader::CDATA
|| $reader->nodeType == XMLReader::WHITESPACE
|| $reader->nodeType == XMLReader::SIGNIFICANT_WHITESPACE) {
$input .= $reader->value;
}
else if ($reader->nodeType == XMLReader::END_ELEMENT
&& $reader->name == "double") {
break;
}
}
break;
}
}
$reader->close();
But how it make work with my code, and how the source should look like, If I need to read a XML.GZ
Thanks for any help!