本地服务器和远地服务器上都有一个字段几乎相同的数据库表。 现在本地服务器需要定期接收远地服务器发送过来的XML数据文件,使用php读取xml中的数据,然后转存到本地服务器数据库表中。 下面是一个XML范例test.xml
<?xml version="1.0" encoding="gb2312" ?> 
<res>
   <is_success result="true" /> 
  <order_list count="1">
<order>
       <order_time>20091213 075817</order_time> 
       <merchant_id>luxi</merchant_id> 
       <order_code>209121301580</order_code> 
                      <stat>yes</order_code>
   </order> <order>
       <order_time>20091113 072317</order_time> 
       <merchant_id>jihu</merchant_id> 
       <order_code>318101301281</order_code> 
                      <stat>no</order_code>
   </order>
           </order_list>
</res>数据库中有字段order_time,merchant_id,order_codestat,本地数据库表通过字段order_code和merchant_id来标识唯一记录,XML转存MySQL过程中可能会执行以下操作: 
1、搜索MySQL数据库表记录;
2、如果记录不存在,则插入; 
3、如果表中已存在记录,则进行 stat 字段值比较,如果 stat 字段值已发生变化,则更改字段值。php读取XML的代码我已经写好
$doc =  simplexml_load_file('test.xml');foreach ($doc->order_list->order as $order) {
  /* 获取xml中的数据 */
  $order_time = $order->order_time;
  $merchant_id = $order->merchant_id;
  $order_code = $order->order_code;
  $stat = $order->stat;  /* 转存 MySQL 代码..... */

MySQL数据库转存操作可通过 REPLACE 语句或者insert ... on duplicate update 来实现,这个我不是很熟,不知各位能否帮忙写段php代码?

解决方案 »

  1.   

    叫人帮忙写代码是一件很不好的事情,也没有人有这个闲情,求人不如求己,还是自己研究研究,试着写了一段代码。各位赏脸点评一下
    $doc =  simplexml_load_file('test.xml');foreach ($doc->order_list->order as $order) {
      /* 获取xml中的数据 */
      $order_time = $order->order_time;
      $merchant_id = $order->merchant_id;
      $order_code = $order->order_code;
      $stat = $order->stat;  /* 转存 MySQL 代码..... */
      $insertSQL = "REPLACE INTO members (order_time, merchant_id, order_code, stat) VALUES ('$order_time ', '$merchant_id', '$order_code', '$stat')";
      $Result1 = mysql_query($insertSQL, $member) or die(mysql_error());
      

  2.   

    建议你用mysql的主从服务器功能.
    这样可以即时更新数据,
    比你先生成xml再解析再入库效率和安全性上都是非常大的提高.详细配置,参看我的blog
    mysqld_muti配置运行mysql多服务
      

  3.   

    不好意思哈,上个链接是配置mysql多服务的!http://www.google.cn/search?hl=zh-CN&source=hp&q=mysql%E4%B8%BB%E4%BB%8E%E6%9C%8D%E5%8A%A1%E5%99%A8%E9%85%8D%E7%BD%AE&aq=f&oq=
      

  4.   

    很遗憾,问题是远地服务器不归我们管,远地服务器只是提供API接口而已,我们只能读取再转存。