Jump to content

Parsing XML and saving to mySQL database


swib30

Recommended Posts

I'm looking to parse an external XML and save it to a mySQL database.

 

Here is a sample of what the XML feed is like:

<?xml version="1.0" encoding="UTF-8"?>
<products type="array">
  <product>
    <body>Product description goes here.</body>
    <body-html>Production description with html goes here.</body-html>
    <created-at type="datetime">2009-12-13T19:41:30-05:00</created-at>
    <handle>cool-sony-speakers</handle>
    <id type="integer">11601262</id>
    <product-type>Speakers</product-type>
    <published-at type="datetime">2009-12-13T19:41:30-05:00</published-at>
    <template-suffix nil="true"></template-suffix>
    <title>Cool Sony Speakers</title>
    <updated-at type="datetime">2009-12-26T03:13:15-05:00</updated-at>
    <vendor>Sony</vendor>
    <tags>Portable, Mobile</tags>
    <variants type="array">
      <variant>
        <compare-at-price type="decimal">49.99</compare-at-price>
        <created-at type="datetime">2009-12-13T19:41:30-05:00</created-at>
        <fulfillment-service>manual</fulfillment-service>
        <grams type="integer">0</grams>
        <id type="integer">29582592</id>
        <inventory-management>shopify</inventory-management>
        <inventory-policy>continue</inventory-policy>
        <inventory-quantity type="integer">1000</inventory-quantity>
        <option1>Black</option1>
        <option2 nil="true"></option2>
        <option3 nil="true"></option3>
        <position type="integer">1</position>
        <price type="decimal">39.00</price>
        <product-id type="integer">11601262</product-id>
        <requires-shipping type="boolean">true</requires-shipping>
        <sku>07-a</sku>
        <taxable type="boolean">true</taxable>
        <title>Black</title>
        <updated-at type="datetime">2009-12-23T00:22:51-05:00</updated-at>
      </variant>
      <variant>
        <compare-at-price type="decimal">49.99</compare-at-price>
        <created-at type="datetime">2009-12-13T19:47:28-05:00</created-at>
        <fulfillment-service>manual</fulfillment-service>
        <grams type="integer">0</grams>
        <id type="integer">29582702</id>
        <inventory-management>shopify</inventory-management>
        <inventory-policy>continue</inventory-policy>
        <inventory-quantity type="integer">0</inventory-quantity>
        <option1>White</option1>
        <option2 nil="true"></option2>
        <option3 nil="true"></option3>
        <position type="integer">2</position>
        <price type="decimal">39.00</price>
        <product-id type="integer">11601262</product-id>
        <requires-shipping type="boolean">true</requires-shipping>
        <sku>07-b</sku>
        <taxable type="boolean">true</taxable>
        <title>White</title>
        <updated-at type="datetime">2009-12-23T00:23:02-05:00</updated-at>
      </variant>
    </variants>
    <images type="array">
      <image>
        <created-at type="datetime">2009-12-23T00:41:21-05:00</created-at>
        <id type="integer">24443102</id>
        <position type="integer">6</position>
        <product-id type="integer">11601262</product-id>
        <updated-at type="datetime">2009-12-23T00:41:21-05:00</updated-at>
        <src>http://cdn.example.com</src>
      </image>
    </images>
    <options type="array">
      <option>
        <name>Color</name>
      </option>
    </options>
  </product>
</products>

 

This is a sample of one product from the XML that contains multiple products. Each 'product' has multiple 'variants' and I only need to save info from the variants to the database. I need to save the following info for each variant:

 

products>product>variants>variant>"id"

products>product>variants>variant>"inventory-quantity"

products>product>variants>variant>"price"

products>product>variants>variant>"product-id"

products>product>variants>variant>"sku"

 

Please help me create a PHP script that can parse this info from the XML and save it to a MySQL database. Just pretend the database is already set up and each value corresponds to a matching database column. For example, "sku" from the XML will be saved in the "sku" column of the database.

 

You can also just pretend the XML file is remotely located at 'http://www.example.com/data.xml'.

 

Thank you very much for the help.

Do you have any current code? If not you probably will not get much help from this forum. As this forum is for people who are having issues with scripts they code. So either give it a try and when you get hung up use the forums for that question or post this in the freelance and offer to pay someone to create it.

 

 

I recommend using the xml parser (so that you can parse the file line by line instead of needing to read the whole thing into memory at once) - http://us3.php.net/xml

 

You will need to wrap the call-back functions in an OOP class of your own so that you can cleanly pass the 'present state' information between the different call-back functions.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.