Welcome to WebmasterWorld Guest from 107.20.20.39

Forum Moderators: httpwebwitch

Message Too Old, No Replies

Save XML in a MySQL database?

   
1:55 am on Aug 1, 2011 (gmt 0)

WebmasterWorld Senior Member sgt_kickaxe is a WebmasterWorld Top Contributor of All Time 5+ Year Member



I have an XML file with several thousand products each using the same simple xml structure, This is one product...

<Category>
<CategoryID>12345</CategoryID>
<CategoryLevel>1</CategoryLevel>
<CategoryName>widgets</CategoryName>
<CategoryParentID>2468</CategoryParentID>
</Category>

The database has catid, catlevel, catname and parentid tables.

How do I parse this file into my database so that each <Category>data</Category> entry in the xml file is saved in its own row on the database? (CategoryID is different for each product)

I've poured over Google and these forums but there just doesn't seem to be a simple answer, preferably in php, that gives an example of the mysql query to use. I wish there was a sticky on XML to MySQL basics.

I don't have a semi-working best effort, yet, any guidance most welcome.
6:55 am on Aug 1, 2011 (gmt 0)

WebmasterWorld Senior Member sgt_kickaxe is a WebmasterWorld Top Contributor of All Time 5+ Year Member



Best effort thus far (and problem resolved)

<?php
include 'example.php'; /the xml file converted to php

$xml = new SimpleXMLElement($xmlstr);

foreach ($xml->Category as $catdata) {
echo $catdata->CategoryID, ' is ', $catdata->CategoryName, PHP_EOL;
}
?>


Obviously that spits out a long string of 'category number is category name', it's just a parsing example, but it works and I can assign values and upload data from there.

just seeing a bit of code sometimes helps, the above did it for me as I was missing SimpleXMLElement.