duanjing2013 2012-01-20 08:04
浏览 87
已采纳

PHP代码 - 将Web服务器(/ public_html / ctrackxml)中的xml文件导入mysql数据库

I have xml files been imported onto an FTP server. This is stored in location '/public_html/ctrackxml/' with a random file name and in the following format:

<message type="POSITIONDATA">
  <messageid>-1</messageid>
  <mobile>SNK261GP</mobile>
  <time>2012/01/20 08:34:45 AM</time>
  <latitude>-29.8477</latitude>
  <longitude>30.9554</longitude>
  <status>Driving</status>
  <speed>82</speed>
  <address> near Outer Ring Road (N2); Umkumbaan; in Durban</address>
  <direction>
  </direction>
  <runningodo>1587000</runningodo>
</message>

I need to loop through all the files in the folder and import each file into the MySQL database table xmldata which has the following structure:

MySQL table

I need each tag in the xml file to be imported into a seperate field in the table. so each xml file represent one table entry.

From the research I have done it looks like I need to use the 'LOAD XML LOCAL INFILE' mysql syntax however I cant seem to get this to work correctly directly within mysql.

If you could point me in the right direction I would greatly appreciate it.

Update

below is the code that I hav emanaged to scrape together with the assistance of another website. http://www.phpfreaks.com/forums/index.php?topic=244744.0

I tested the script from phpfreaks and it works 100% however the xml structure is quite different. I have tried to modify the code to suite my xml file but am having some issues to get this working.

my code is as follows but currently fails ont he foreach statement:

<?php

echo "starting <br><br>";
//mysql connection
$con2 = mysql_connect("localhost","dbuser","dbpassword");
if (!$con2)  {  die('Could not connect: ' . mysql_error());  }
$selectdb = mysql_select_db("dbname", $con2);
if (!$selectdb)  { die('Database not used: ; ' . mysql_error());  }

echo "connected to DB<br/><br/>";

//simplexml load xml file   
$library =  simplexml_load_file('http://www.website/ctrackxml/CTO_20120119140006_0000.xml');

echo "xml loaded<br/><br/>";

//loop through parsed xmlfeed and print output      

foreach ($message->message as $message) {                  
printf("messageid: %s
", $messageid->messageid);                  
printf("mobile: %s
", $mobile->mobile);
printf("time: %s
", $time->time);
printf("latitude: %s
", $latitude->latitude);
printf("longitude: %s
", $longitude->longitude);
printf("status: %s
", $status->status);
printf("speed: %s
", $speed->speed);
printf("address: %s
", $address->address);
printf("direction: %s
", $direction->direction);
printf("runningodo: %s
", $runningodo->runningodo);

echo "xml parsed<br/><br/>";

//insert into databse                     
mysql_query("INSERT INTO xml (messageid, mobile, time,latitude,longitude,status,speed,address,direction,odometer)
VALUES (\"$messageid->messageid\", \"$mobile->mobile\", \"$time->time\", \"$latitude->latitude\", \"$longitude->longitude\", \"$status->status\", \"$speed->speed\", \"$address->address\", \"$direction->direction\", \"$runningodo->runningodo\")")
or die(mysql_error());

echo "inserted into mysql<br/><br/>";

//show updated records            
printf ("Records inserted: %d
", mysql_affected_rows());  
}


//close connection 
mysql_close($con2);

?>

Thanks as always to everyone for the help.

  • 写回答

5条回答 默认 最新

  • duanfangbunao36970 2012-01-21 00:48
    关注
    <?php
    
    ini_set('display_errors','On');
    
    echo "starting";
    
    //mysql connection
    $con2 = mysql_connect("localhost","dbuser","dbpass");
    if (!$con2)  {  
        die('Could not connect: ' . mysql_error());  
    }
    
    $selectdb = mysql_select_db("dbname", $con2);
    if (!$selectdb)  { 
        die('Database not used: ; ' . mysql_error());  
    }
    
    echo "connected to DB<br /><br />";
    
    // Read filenames in current directory looking for XML files
    $file_arr = array();
    if ($handle = opendir('.')) {
        while (false !== ($file = readdir($handle))) {
            if (($file != ".") && ($file != "..")) {
                if(substr($file, -4) == ".xml")
                {
                    array_push($file_arr, $file);
                }
            }
        }
        closedir($handle);
    }
    
    // Loop through each XML file in the current directory
    foreach($file_arr as $filename)
    {
        //simplexml load xml file   
        $mess = simplexml_load_file($filename);
        echo "xml loaded<br /><br />";
    
        $messageid = mysql_real_escape_string($mess->messageid);
        $mobile = mysql_real_escape_string($mess->mobile);
        $time = mysql_real_escape_string($mess->time);
        $latitude = mysql_real_escape_string($mess->latitude);
        $longitude = mysql_real_escape_string($mess->longitude);
        $status = mysql_real_escape_string($mess->status);
        $speed = mysql_real_escape_string($mess->speed);
        $address = mysql_real_escape_string($mess->address);
        $direction = mysql_real_escape_string($mess->direction);
        $runningodo = mysql_real_escape_string($mess->runningodo);
    
        echo "xml parsed<br /><br />";
    
        //insert into databse                     
        mysql_query("INSERT INTO xml (messageid, mobile, time, latitude, longitude, status, speed, address, direction, odometer)
        VALUES ('$messageid', '$mobile', '$time', '$latitude', '$longitude', '$status', '$speed', '$address', '$direction', '$runningodo')")
        or die(mysql_error());
    
        echo "inserted into mysql<br /><br />";
    
        //show updated records            
        printf ("Records inserted: %d
    ", mysql_affected_rows());  
    }
    //close connection 
    mysql_close($con2);
    
    ?>
    

    The above code should achieve what you are looking for based on the spec. As stated there is one XML record per one XML file.

    This code is designed to run in the same directory as the XML files, you can easily change this by editing opendir command in the code. It will read all the XML files in the current working directory and place the data into the Database.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值