开发者

Importing several XML files (with the same elements) into combined MySQL tables

开发者 https://www.devze.com 2023-02-22 10:40 出处:网络
I have several XML files, all of the format: <story id=\"12349\"> <name>Reviews</name>

I have several XML files, all of the format:

 <story id="12349">
   <name>Reviews</name>
   <slug>reviews</slug>
   <categories>
     <category primary="1">cat1</category>
     <category>cat2</category>
   </categories>
 </story>
开发者_开发问答

How can I import them all into a MySQL database (ideally using PHP, as this is what I know best) which will let me edit the <category> elements? I'm imagining I want one table with a field for the id and a field for the complete XML, and another with a field for the id and fields for each of the <category> elements, though am open to any other suggestions.

Many thanks...


Try out simple xml to parse the xml into an array which you can then push to a DB with simple insert statments within a foreach loop.

http://www.w3schools.com/php/php_xml_simplexml.asp


You need to create the following table structures:

Stories table { name => varchar , slug => varchar , id => int }
Categories table {name => varchar, id => int}
LinkStoryCategories table { story_id => int , category_id => int }

Then you need to parse the XML files using PHP DomDocument http://php.net/manual/en/class.domdocument.php

0

精彩评论

暂无评论...
验证码 换一张
取 消