douren1928
2014-08-22 13:51
浏览 37

将xml数据导入mysql数据库

i need to import this xml data into mysql database.. i dont have clear idea about it.

this is my .xml file

<employee>
        <row>
            <field name="employee_name">Syed</field>
            <field name="employee_code">101</field>
            <field name="employee_email">syed@gmail.com</field>
            <field name="employee_designation">Trainee</field>
            <field name="employee_number">7412347798</field>
            <field name="employee_salary">6000</field>
            <field name="employee_age">21</field>
        </row>
        <row>
            <field name="employee_name">Hari</field>
            <field name="employee_code">102</field>
            <field name="employee_email">hari@gmail.com</field>
            <field name="employee_designation">Trainee</field>
            <field name="employee_number">9895852525</field>
            <field name="employee_salary">6000</field>
            <field name="employee_age">23</field>
        </row>
        <row>
            <field name="employee_name">Raja</field>
            <field name="employee_code">103</field>
            <field name="employee_email">raja@gmail.com</field>
            <field name="employee_designation">Trainee</field>
            <field name="employee_number">9956853458</field>
            <field name="employee_salary">6000</field>
            <field name="employee_age">23</field>
        </row>
        <row>
            <field name="employee_name">imran</field>
            <field name="employee_code">104</field>
            <field name="employee_email">imran@gmail.com</field>
            <field name="employee_designation">Trainee</field>
            <field name="employee_number">9836052525</field>
            <field name="employee_salary">6000</field>
            <field name="employee_age">21</field>
        </row>
        <row>
            <field name="employee_name">Sam</field>
            <field name="employee_code">105</field>
            <field name="employee_email">sam@gmail.com</field>
            <field name="employee_designation">Trainee</field>
            <field name="employee_number">8625852525</field>
            <field name="employee_salary">6000</field>
            <field name="employee_age">21</field>
        </row>
        <row>
            <field name="employee_name">vinod</field>
            <field name="employee_code">106</field>
            <field name="employee_email">vinod@gmail.com</field>
            <field name="employee_designation">Trainee</field>
            <field name="employee_number">7225852525</field>
            <field name="employee_salary">6000</field>
            <field name="employee_age">24</field>
        </row>
    </employee>

my routine for this function is,

 if ($file) {
                $handle = simplexml_load_file($file);                           //  Open the file and read
                while($strBookData = fgets($handle, 4096)) {        //  To get Array 
                    $strDatas[] = $strBookData;
                    $strTableColumn = count($strBookData);              // To Get Column count
                }

i dont know what to do further.. can any one give me suggestions?? thanks in advance :)

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douzhan4522 2014-08-22 14:26
    已采纳

    You simply have to get all rows as an array. Just do it in a loop. And then update the table. Like so:

    while($row = $this->nextRow()) {
        mysql_query("INSERT INTO table (name, code) VALUES ($row['name']), $row['code']");
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题