简体   繁体   中英

Edit XML data and import to SQL table

I have an XML feed from a wholesaler that is updated periodically to show the stock quantities of the items they wholesale. I need some help in collecting that XML file data twice a day and importing it into the SQL table on my website. Here is an example of the XML file:

<?xml version='1.0' encoding='ISO-8859-1'?>
<!DOCTYPE stockreport SYSTEM
'http://www.wholesalewebsite/feeds/status.dtd'>
<stockreport>
    <timestamp>14/07/2014 18:55:26</timestamp>
<version>2.0</version>
    <products>
            <product type="main">
                     <code>M0507</code>
                     <name>Example Product</name>
                     <stock>4</stock>
                     <status>In Stock</status>
            </product>
     </products>
</stockreport>

I need to use this feed to update the quantities of the items in my store. My SQL table is called prods_stock and in the table the code (M0507) is called customid and stock is called quantity, so I need to alter the XML fields to match my existing table fields and then update the quantities using the customid(code) as an anchor point. I can ignore name and status.

How would I go about doing this?

Thanks in advance

Thanks for the suggestions, as I am not very good with coding etc... I have decided to just manually insert an sql query daily to update the database. An example of the code I will use is: UPDATE products_stock SET customid = 'n12',quantity = 10 WHERE 1=1 AND customid= 'n12'; UPDATE products_stock SET customid = 'n50',quantity = 10 WHERE 1=1 AND customid= 'n50';

I will be using a csv feed file from the wholesaler and getting the info from there. Is there an easier/quicker way to drag the info from the csv file and update the table, rather than manually type the code for each line?

Thanks

You may have a look at XSLT . That allows you to parse any XML file and transform it's contents into any form you like. Using that you could transform the XML into SQL queries and run them agains the database.

You could then write a small shell script that loads the XML file, runs the XST-rules and hands the result on to the database.

You cannot archive that without any programming knowledge, but XSLT ist quite simple for such simple use cases. First I would work through a basic XSLT tutorial to become familar with the basics and then try it out with our problem.

Happy learning.. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM