Coding For Online Success

Parsing XML CJ Product Feeds


I mentioned on May 29th that CJ allows you to create and manage product catalogs now. This is great. You can create a product feed and have it available in your FTP directory (or whatever delivery method you wish). For my personal set up, I have it stored on their server, and I need to FTP to it and grab it. I do everything via PHP on a scheduled intervals.

I wanted to show you how you can grab the XML data and parse it. You could use it directly or push it into a database, etc. In my situation, I want to update a local database with product items. Items will be updated every week. I won’t go into the database piece (let me know if you need me to go into INSERT/DELETE SQL examples).

For this example, you will need PHP5 since we’ll be using the simplexml_load_file() function. This is assuming that you’ve already FTP’d the XML to your working directory. Again, let me know if you need this process as well, and I can provide some examples.

Once we have the XML file, we pass it to the simplexml_load_file function. This function quickly parses the XML into a object. Since it’s a object, it’s snap to reference the values. My example loops through the XML object and sets local variables with the values from the XML file. You could then INSERT this values into a database or display them in real time.

span class=”st0″>’1111111_11111_20090528.xml’

My version of MySQL is 5, if you’re running 5.1 or 6, you can use a sweet MySQL command called “LOAD XML LOCAL INFILE”. For example, you could run this and it would populate your database table very quickly.

span class=”st0″>"LOAD XML LOCAL INFILE ‘1111111_11111_20090528.xml’
                INTO TABLE tblProductsTest
                ROWS IDENTIFIED BY ‘ ‘""

I think the ability to quickly make some product feeds is awesome. Now, I just need to find time to create some new store fronts and make some cash. If you find this post useful, please follow me on Twitter and Digg this post!


Leave a Reply

Required fields are marked *.

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.