duanma8207 2015-07-21 16:37
浏览 52
已采纳

将simplexml解析为MYSQL

I have an api, which I send to a database and the results are returned in an xml format, this works fine and i can output the results to the screen with no problem. The xml feed is along list of property details. What i am trying to do is store the results in a mysql database, using the code below.

  $feeds = array('http://web.demo.net/ademo_search.xml?  &upw=123456');
 foreach( $feeds as $feed ) {
$xml = simplexml_load_file($feed);

foreach($xml->channel->item as $item)
{

mysql_query("INSERT INTO property1 (id, department, branch, address1) 
VALUES (
    '', 
    '".mysql_real_escape_string($item->id)."', 
    '".mysql_real_escape_string($item->department)."', 
    '".mysql_real_escape_string($item->branch)."', 
    '".mysql_real_escape_string($item->address1)."')");       
     }
  }

When I run this code I don't get an errors, nor does the data get added to the database.

here is a link to the xml structure, as you will see for my test i am only trying to insert the first few items.

  • 写回答

2条回答 默认 最新

  • duancan2539 2015-07-21 18:38
    关注

    This is exactly what you need :)

    I hope you like it

    $feed = "test.xml";
    $xml = simplexml_load_file($feed);
    
    $arr = array();
    $i = 0;
    foreach($xml->houses->property as $item){
        $arr[$i][] = (string) $item->id;
        $arr[$i][] = (string) $item->department;
        $arr[$i][] = (string) $item->branch;
        $arr[$i][] = (string) $item->address->address1;
        $i++;
    }
    var_dump($arr); // now you have them in an array .. store them in db ;)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站