tog22
tog22

Reputation: 526

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...

Upvotes: 0

Views: 676

Answers (2)

ysrb
ysrb

Reputation: 6740

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

Upvotes: 0

uadnal
uadnal

Reputation: 11445

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

Upvotes: 1

Related Questions