doutao1282 2010-10-17 16:25
浏览 77

寻找一种方法来解析并使用php将XML字符串插入MySQL

I am looking for a way to parse and insert following XML string data to MySQL tables using php.. please help

<?xml version="1.0" encoding="UTF-8"?><brandModelListResponse>  
<brand ID="Nokia" Description="Nokia">  
    <model ID="N93i" OS="Symbian" OSVersion="SymS60V3" image="nokia-n93i.gif">N93i</model>  
    <model ID="N95" OS="Symbian" OSVersion="SymS60V3" image="nokia-n95.gif">N95</model>  
    <model ID="Nokia300" OS="Symbian" OSVersion="SymS60V3" image="">Nokia300</model>  
</brand>
<brand ID="Motorola" Description="Motorola">  
    <model ID="E1070" OS="J2ME" OSVersion="Motorola J2ME A.5" image="'E1070.jpg">E1070</model>  
    <model ID="E398" OS="J2ME" OSVersion="Motorola J2ME A.1" image="">E398</model>  
</brand>  

I have two tables, one for brands and other models.. need to insert all the data available in above xml to these tables..

Thanks in advance
Ansar

  • 写回答

2条回答 默认 最新

  • douyan4958 2010-10-17 16:34
    关注

    In order to collect the data from XML and save it into a database, two steps needs to be performed. First, you need to read the XML and store it into some PHP data structures. Secondly, you need to store these data structures into a database.

    Assuming you are using a decently fresh PHP (>=5) you just:

    So begin with reading the data with an XML parser. And once you have done that, continue with a MySQL library. In order to store data to a MySQL database, some rudimentary SQL knowledge is needed. Consult for instance W3School's MySQL tutorial.

    Of course, you need to do the final coding yourself. But just as a simple structural proposal, your script might look something like this:

    $xml_data = file_get_contents('file.xml');
    $xml = new SimpleXMLElement($xml_data); 
    
    foreach ($xml->item as $item)
    {
      $sql = 'INSERT INTO table (...) VALUES ('.$item['name'].');';
      mysql_query(...); //
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错