用 PHP 读取的批量 XML 文件

Posted

技术标签:

【中文标题】用 PHP 读取的批量 XML 文件【英文标题】:Bulk XML file read in PHP 【发布时间】:2016-05-25 23:43:04 【问题描述】:

我是 php 新手。

我的客户分享了 5GB XML 产品信息数据。

我需要在 DB 中导入这些数据。

我已经使用了下面的代码并在浏览器中运行。

<?php 
ini_set('max_execution_time', 0);
ini_set('memory_limit', '6144M');

$mysql_hostname = "localhost";
$mysql_user     = "XXXX";
$mysql_password = "XXXX";
$mysql_database = "XXXX";

$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Oops some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Oops some thing went wrong");

$languages = simplexml_load_file("sample.xml");
$total_row =  count($languages->entry);
$data = $languages->entry;

foreach($data as $key => $value) 
    $title = $value->title; 
    $sql   = "INSERT INTO `YYYY` VALUES ( NULL, '$title')";
    $run = mysql_query($sql);  

echo "Completed ...... !";
?>

我收到以下错误消息。

Apache 错误日志:

[Mon Feb 15 12:24:40.140480 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00428: Parent: child process 5244 exited with status 3221225477 -- Restarting.
[Mon Feb 15 12:24:40.510501 2016] [ssl:warn] [pid 4752:tid 260] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Feb 15 12:24:40.567504 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00455: Apache/2.4.17 (Win32) OpenSSL/1.0.2d PHP/5.5.30 configured -- resuming normal operations
[Mon Feb 15 12:24:40.567504 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00456: Apache Lounge VC11 Server built: Oct 13 2015 10:54:13
[Mon Feb 15 12:24:40.567504 2016] [core:notice] [pid 4752:tid 260] AH00094: Command line: 'c:\\xampp\\apache\\bin\\httpd.exe -d C:/xampp/apache'
[Mon Feb 15 12:24:40.569505 2016] [mpm_winnt:notice] [pid 4752:tid 260] AH00418: Parent: Created child process 5032
[Mon Feb 15 12:24:40.946526 2016] [ssl:warn] [pid 5032:tid 272] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Feb 15 12:24:41.091534 2016] [ssl:warn] [pid 5032:tid 272] AH01909: www.example.com:443:0 server certificate does NOT include an ID which matches the server name
[Mon Feb 15 12:24:41.121536 2016] [mpm_winnt:notice] [pid 5032:tid 272] AH00354: Child: Starting 150 worker threads. 

如何做到这一点??

【问题讨论】:

***.com/questions/15363901/… 你能检查一下 apache 日志吗.. 发生了什么? 我已经更新了我的问题,请检查一下.. 你能把它分块吗? 【参考方案1】:

您可以提高性能并将所有查询传递给mysql一次:

function str_lreplace($search, $replace, $subject)

    $pos = strrpos($subject, $search);

    if($pos !== false)
    
        $subject = substr_replace($subject, $replace, $pos, strlen($search));
    

    return $subject;


$sql   = "INSERT INTO `YYYY` VALUES ";
foreach($data as $key => $value) 
    $title = $value->title; 
    $sql   =$sql. "( NULL, '$title'),";
    //$run = mysql_query($sql);  

//replace last occurrence of a  '),'  with  ');'  in php
$sql = str_lreplace('),',');', $sql);
$run = mysql_query($sql);  
echo "Completed ...... !";

【讨论】:

以上是关于用 PHP 读取的批量 XML 文件的主要内容,如果未能解决你的问题,请参考以下文章

R语言XML文件

PHP读取及生成xml文件实测

php读取文件使用redis的pipeline(管道)导入大批量数据

php如何获取文件内容?

PHP读取XML文件数据

php使用cURL读取XML字节