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条)

报告相同问题?

悬赏问题

  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟