homepage Welcome to WebmasterWorld Guest from 107.22.78.233
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Save XML in a MySQL database?
Sgt_Kickaxe




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

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.

 

Sgt_Kickaxe




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved