简体   繁体   中英

insert xml file into sql

I want to insert Large xml values into sql using php.

This is xml example :

  <?xml version="1.0" encoding="utf-8" ?> 
<contacts>
<con>
  <id>1</id> 
  <name>user_name</name> 
  <city>city_name</city> 
  <street>street_name</street> 
  <phone1>phone_number1</phone1> 
  <phone2>phone_number2</phone2> 
  <phone3 /> 
  <phone4 /> 
  </con>


  <con>
  <id>2</id> 
  <name>user_name</name> 
  <city>city_name</city> 
  <street>street_name</street> 
  <phone1>phone_number1</phone1> 
  <phone2>phone_number2</phone2> 
  <phone3 /> 
  <phone4 /> 
  </con>

    </contacts>

I make SQL like this so i want insert the data in it :

CREATE TABLE IF NOT EXISTS `contacts` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(250) NOT NULL,
  `city` varchar(250) NOT NULL,
  `street` varchar(250) NOT NULL,
  `phone_1` text NOT NULL,
  `phone_2` text NOT NULL,
  `phone_3` text NOT NULL,
  `phone_4` text NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=cp1256 AUTO_INCREMENT=1 ;

Can I do this job with php? and how can I do it?

Thanks

First of all phone_3 and phone_4 are NOT NULL in the database, and not set in the XML.

You can do this by parsing the xml in your php code with the XML Parser ( alternative reference ). After that it's simply inserting them into the database.

是的,您可以使用xml函数xml_parse_into_struct()将xml转换为数组,而不是使用循环可以在数据库中插入数据。

解析xml文件,为每个数据构造一个sql查询,执行查询,输出一些验证,例如查询是否成功...

As dragon112 mentions you want to parse the XML in your PHP code. A similar question has already been asked and answered. Take a look here for some inspiration.

Good luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM