Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php将xml文件导入mysql数据库_Php_Mysql_Xml - Fatal编程技术网

使用php将xml文件导入mysql数据库

使用php将xml文件导入mysql数据库,php,mysql,xml,Php,Mysql,Xml,以下数据是我的xml文件。如何使用php将其导入mysql数据库 <?xml version="1.0" encoding="utf-8"?> <title name="employee_details"> <Item> <field name="employee_name">Hari</field> <field name="employee_code">101</field&

以下数据是我的xml文件。如何使用php将其导入mysql数据库

<?xml version="1.0" encoding="utf-8"?>
<title name="employee_details">
    <Item>
        <field name="employee_name">Hari</field>
        <field name="employee_code">101</field>
        <field name="employee_email">hari@gmail.com</field>
        <field name="employee_designation">Trainee</field>
        <field name="employee_salary">6000</field>
    </Item>
    <Item>
        <field name="employee_name">Syed</field>
        <field name="employee_code">102</field>
        <field name="employee_email">syed@gmail.com</field>
        <field name="employee_designation">Trainee</field>
        <field name="employee_salary">6000</field>
    </Item>
    <Item>
        <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_salary">6000</field>
    </Item>
    <Item>
        <field name="employee_name">Murali</field>
        <field name="employee_code">104</field>
        <field name="employee_email">murali@gmail.com</field>
        <field name="employee_designation">Trainee</field>
        <field name="employee_salary">6000</field>
    </Item>
</title>

哈里
101
hari@gmail.com
练习生
6000
赛义德
102
syed@gmail.com
练习生
6000
拉贾
103
raja@gmail.com
练习生
6000
穆拉里
104
murali@gmail.com
练习生
6000

请帮我导入。

$code>$string=您可以执行mysql查询从XML文件导入数据

$string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<title name="employee_details">
    <Item>
        <field name="employee_name">Hari</field>
        <field name="employee_code">101</field>
        <field name="employee_email">hari@gmail.com</field>
        <field name="employee_designation">Trainee</field>
        <field name="employee_salary">6000</field>
    </Item>
    <Item>
        <field name="employee_name">Syed</field>
        <field name="employee_code">102</field>
        <field name="employee_email">syed@gmail.com</field>
        <field name="employee_designation">Trainee</field>
        <field name="employee_salary">6000</field>
    </Item>
</title>
XML;

$xml = simplexml_load_string($string);

print_r($xml);
LOAD XML LOCAL INFILE 'employee.xml' INTO TABLE employee;
单击查看更多详细信息

$strContents = file_get_contents($file);
$strDatas = Xml2Array($strContents);
$strEmployeeData = $strDatas['employee']['row'];
$strTableColumn = count($strEmployeeData[0]);
Xml2Array的函数:

function Xml2Array($contents, $get_attributes=1, $priority = 'tag') {
    if(!$contents) return array();

    if(!function_exists('xml_parser_create')) {
        //print "'xml_parser_create()' function not found!";
        return array();
    }

    //Get the XML parser of PHP - PHP must have this module for the parser to work
    $parser = xml_parser_create('');
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
    xml_parse_into_struct($parser, trim($contents), $xml_values);
    xml_parser_free($parser);

    if(!$xml_values) return;//Hmm...

    //Initializations
    $xml_array = array();
    $parents = array();
    $opened_tags = array();
    $arr = array();

    $current = &$xml_array; //Refference

    //Go through the tags.
    $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
    foreach($xml_values as $data) {
        unset($attributes,$value);//Remove existing values, or there will be trouble

        //This command will extract these variables into the foreach scope
        // tag(string), type(string), level(int), attributes(array).
        extract($data);//We could use the array by itself, but this cooler.

        $result = array();
        $attributes_data = array();

        if(isset($value)) {
            if($priority == 'tag') $result = $value;
            else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
        }

        //Set the attributes too.
        if(isset($attributes) and $get_attributes) {
            foreach($attributes as $attr => $val) {
                if($priority == 'tag') $attributes_data[$attr] = $val;
                else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
            }
        }

        //See tag status and do the needed.
        if($type == "open") {//The starting of the tag '<tag>'
            $parent[$level-1] = &$current;
            if(!is_array($current) or (!in_array($tag, array_keys($current)))) { //Insert New tag
                $current[$tag] = $result;
                if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
                $repeated_tag_index[$tag.'_'.$level] = 1;

                $current = &$current[$tag];

            } else { //There was another element with the same tag name

                if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
                    $repeated_tag_index[$tag.'_'.$level]++;
                } else {//This section will make the value an array if multiple tags with the same name appear together
                    $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
                    $repeated_tag_index[$tag.'_'.$level] = 2;

                    if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well
                        $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                        unset($current[$tag.'_attr']);
                    }

                }
                $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
                $current = &$current[$tag][$last_item_index];
            }

        } elseif($type == "complete") { //Tags that ends in 1 line '<tag />'
            //See if the key is already taken.
            if(!isset($current[$tag])) { //New Key
                $current[$tag] = $result;
                $repeated_tag_index[$tag.'_'.$level] = 1;
                if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;


            } else { //If taken, put all things inside a list(array)
                if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...

                    // ...push the new element into that array.
                    $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;

                    if($priority == 'tag' and $get_attributes and $attributes_data) {
                        $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                    }
                    $repeated_tag_index[$tag.'_'.$level]++;

                } else { //If it is not an array...
                    $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
                    $repeated_tag_index[$tag.'_'.$level] = 1;
                    if($priority == 'tag' and $get_attributes) {
                        if(isset($current[$tag.'_attr'])) { //The attribute of the last(0th) tag must be moved as well

                            $current[$tag]['0_attr'] = $current[$tag.'_attr'];
                            unset($current[$tag.'_attr']);
                        }

                        if($attributes_data) {
                            $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
                        }
                    }
                    $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
                }
            }

        } elseif($type == 'close') { //End of tag '</tag>'
            $current = &$parent[$level-1];
        }
    }

    return($xml_array);
}
函数Xml2Array($contents,$get\u attributes=1,$priority='tag'){
if(!$contents)返回数组();
如果(!function_存在('xml_parser_create')){
//打印“'xml_parser_create()'函数未找到!”;
返回数组();
}
//获取PHP的XML解析器-PHP必须具有此模块,解析器才能工作
$parser=xml\u parser\u create(“”);
xml_parser_set_option($parser,xml_option_TARGET_编码,“UTF-8”);#http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss
xml\u parser\u set\u option($parser,xml\u option\u CASE\u FOLDING,0);
xml\u parser\u set\u option($parser,xml\u option\u SKIP\u WHITE,1);
xml_将_解析为_结构($parser,trim($contents),$xml_值);
xml解析器免费($parser);
如果(!$xml\u值)返回;//嗯。。。
//初始化
$xml_array=array();
$parents=array();
$opened_tags=array();
$arr=array();
$current=&$xml\u数组;//引用
//检查一下标签。
$repeated_tag_index=array();//具有相同名称的多个标记将转换为一个数组
foreach($xml_值为$data){
unset($attributes,$value);//删除现有值,否则会有麻烦
//此命令将把这些变量提取到foreach范围中
//标记(字符串)、类型(字符串)、级别(int)、属性(数组)。
extract($data);//我们可以单独使用数组,但是这个更酷。
$result=array();
$attributes_data=array();
if(isset(价值)){
如果($priority=='tag')$result=$value;
else$result['value']=$value;//如果处于“属性”模式,则将该值放入assoc数组中
}
//也设置属性。
if(isset($attributes)和$get_属性){
foreach($attr=>$val的属性){
如果($priority=='tag')$attributes\u data[$attr]=$val;
else$result['attr'][$attr]=$val;//设置名为'attr'的数组中的所有属性
}
}
//查看标记状态并执行所需操作。
如果($type==“open”){//标记“”的开始
$parent[$level-1]=&$current;
如果(!is_array($current)或(!in_array($tag,array_keys($current))){//插入新标记
$current[$tag]=$result;
如果($attributes\u data)$current[$tag.\u attr']=$attributes\u data;
$repeated_tag_index[$tag.'''.$level]=1;
$current=&$current[$tag];
}else{//还有另一个元素具有相同的标记名
if(isset($current[$tag][0]){//如果有第0个元素,它已经是数组了
$current[$tag][$repeated\u tag\u index[$tag.''.'.$level]]=$result;
$repeated_tag_index[$tag.''.$level]++;
}否则{//如果同时出现多个同名标记,则此部分将使该值成为数组
$current[$tag]=array($current[$tag],$result);//这将现有项和新项组合在一起形成一个数组
$repeated_tag_index[$tag.'''.$level]=2;
如果(isset($current[$tag.''u attr']){//最后一个(第0个)标记的属性也必须移动
$current[$tag]['0_attr']=$current[$tag.''u attr'];
未设置($current[$tag.''u attr']);
}
}
$last_item_index=$repeated_tag_index[$tag.'''.$level]-1;
$current=&$current[$tag][$last_item_index];
}
}elseif($type==“complete”){//以1行结尾的标记“”
//看看钥匙是否已经拿了。
如果(!isset($current[$tag]){//New Key
$current[$tag]=$result;
$repeated_tag_index[$tag.'''.$level]=1;
如果($priority==“tag”和$attributes\u data)$current[$tag.\u attr']=$attributes\u data;
}else{//如果采用,则将所有内容放入列表(数组)中
if(isset($current[$tag][0])和is_数组($current[$tag]){//如果它已经是一个数组。。。
//…将新元素推入该数组。
$current[$tag][$repeated\u tag\u index[$tag.''.'.$level]]=$result;
if($priority=='tag'和$get\u attributes和$attributes\u data){
$current[$tag][$repeated\u tag\u index[$tag.''.$level].\u attr']=$attributes\u data;
}
$repeated_tag_index[$tag.''.$level]++;
}否则{//如果它不是数组。。。
$current[$tag]=数组($current[$tag],$result);/…使用现有值和新值将其设置为数组
$repeated_tag_index[$tag.'''.$level]=1;
if($priority=='tag'和$get\u属性){
如果(isset($current[$tag.''u attr']){//最后一个(第0个)标记的属性也必须移动
$current[$tag]['0_attr']=$current[$tag.''u attr'];
未设置($current[$tag.''u attr']);
}
如果($attributes\u data){
$current[$tag][$repeated\u tag\u index[$tag.''.$level].\u attr']=$attributes\u data;