当前位置: 动力学知识库 > 问答 > 编程问答 >

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

问题描述:

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

分享给朋友:
您可能感兴趣的文章:
随机阅读: