我必须为我的商店解析3个远程XML文件(产品、价格、类别),其中最大的文件是关于500MB+的。我必须解析它们并插入mysql数据库。
我可以从两种格式中选择
所以,基本上我有两个选择(我想)
两者我都有麻烦。
products (id、name、parent_id、parent_name、品牌、图像)值('.$item'id'.’、‘.$item’name‘.’‘、’.$item‘’parent_id‘.’,“‘.$item’‘parent_name’.‘”,’‘$item’品牌‘,’.‘}
}}这段代码适用于no mysql查询,但如果我将它们放入,则会引发各种错误。
解析器错误:文档末尾的额外内容
假设我想在products.xml中解析myxml.tar.gz,这可能吗?
$url = "compress.zlib:///myxml.tar.gz";
$reader = new XMLReader();
$reader->open($url);
$reader->read();上面说文件是空的
发布于 2014-02-19 19:59:59
我做了像你一样的事。
我已经从3 xml大文件压缩的web服务压缩档案下载。我是这样做的:我在上面设置了:
ini_set('max_execution_time',1000);//600秒 Ini_set(‘mysql.connecttimeout’,1000);//运行大型sql ini_set('default_socket_timeout',1000);
我将压缩文件下载到临时文件夹:
/**
* Metoda care scrie arhiva pe hardisc
* @param $string textul de scris in fisierul zip
* @return string Calea catre fisiser
*/
private function write_to_file($string)
{
$base = $this->tmpPath;
$date_folder = $base.date('Y_m').DIRECTORY_SEPARATOR.date('d');
if(!file_exists($date_folder))
{
mkdir($date_folder, 0777, TRUE);
}
$file = $date_folder.DIRECTORY_SEPARATOR.'products_'.date("Y_m_d_H_i").'.zip';
// This uses less memory than file_put_contents
$f = fopen($file, 'w');
fwrite($f, $string);
fclose($f);
return $file;
}在此之后,我将xml文件从zip提取到temp文件夹:
public function dezarhiveaza($file)
{
$zip = new ZipArchive;
$res = $zip->open($file);
if ($res === TRUE) {
$zip->extractTo($this->tmpPath);
$zip->close();
$this->write_log('extract success');
} else {
$this->write_log('error ');
}
}接下来,我从XML中提取产品列表,并使用1000行进行插入MySQL查询:
private function deserializeazaForme()
{
$formePath=$this->tmpPath.
"data".DIRECTORY_SEPARATOR.'forme.xml';
$xml = simplexml_load_file($formePath);
$forme = $xml->xpath('//Table');
if($forme)
{
$strFormeInsertFirst="INSERT INTO `forme` (`id`, `denumire`) VALUES ";
$strFormeInsert=$strFormeInsertFirst;
foreach ($forme as $key=>$forma) {
$strFormeInsert .= "(".$forma->id.",'".$forma->denumire."),";
if($key%1000==0 && $key >0){
$strFormeInsert = rtrim($strFormeInsert, ",") ;
$strFormeInsert .=";";
$this->runQuery($strFormeInsert);
$strFormeInsert=$strFormeInsertFirst;
}
}
$strFormeInsert = rtrim($strFormeInsert, ",") ;
$strFormeInsert .=";";
$this->runQuery($strFormeInsert);
}
}https://stackoverflow.com/questions/21885245
复制相似问题