We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 26310
    • 130 Posts
    So I am parsing an XML document of customers. Each customer has associated details; phone, address, email, etc.

    What is the best way to insert this info into a DB? Create an array of customers and within each item is a nested array of details? What about just created one array with the values comma deliminated and throwing that into an insert statement? The customers list is just an example, for my customers I’m using WebLoginPE smiley

    Any help is mucho appreciated.
      I twitch because I care....and drink too much coffee.
      • 26310
      • 130 Posts
      Thanks for the response. So I’ve got the INSERT thing down, basically this is for an XML feed from FoxyCart.

      I tried looking at FoxyBack but it’s more than I need. I simply need to store receipts in a database. The customer info will just be the internal key from the modx users table. So the insert statement will only happen when FC posts the XML to the page right after an order. There may be duplicate info on there but the date will be unique. The thing is, is I’m dealing with a rather lengthy DOM tree and don’t have experience adding that much data into a DB.

      I took a look at the INSERT statement for the MODx API and saw how I could create an arrary easily from the transaction data, however, I’m not sure how to handle a situation like below:
      <foxydata>
        <datafeed_version>XML FoxyCart Version 0.6</datafeed_version>
        <transactions>
          <transaction>
            <order_total>24.38</order_total>
            <order_total>24.38</order_total>
            <customer_password>1aab23051b24582c5dc8e23fc595d505</customer_password>
            <custom_fields>
              <custom_field>
                <custom_field_name>My_Cool_Text</custom_field_name>
                <custom_field_value>Value123</custom_field_value>
              </custom_field>


      I can handle everything in the parent element of transaction but anything deeper than that just returns all node values. For example the value of custom_fields would end up being: My_Cool_Text Value123...

      I’d need the custom field to return it’s own field for DB insertion.
        I twitch because I care....and drink too much coffee.
        • 7155
        • 160 Posts
        can you post the function that parses the xml file?
          • 26310
          • 130 Posts
          I feel like I could loop through the XML nodes starting at the Transaction element. However, I’ve realized that I need to set my shop up first, run a test transaction and see what comes back. The example provided by Foxy Cart doesnt’ seem that accurate and because I’m manually parsing elements I need to know exactly how my particular feed will look.

          Here’s what I’m working with so far:

          <?php
          
          $strXML = <<<XML
          <?xml version='1.0' standalone='yes'?>
          <foxydata>
            <datafeed_version>XML FoxyCart Version 0.6</datafeed_version>
            <transactions>
              <transaction>
                <id>616</id>
                <transaction_date>2007-05-04 20:53:57</transaction_date>
                <customer_id>122</customer_id>
                <customer_first_name>John</customer_first_name>
                <customer_last_name>Doe</customer_last_name>
                <customer_address1>12345 Any Street</customer_address1>
                <customer_address2></customer_address2>
                <customer_city>Any City</customer_city>
                <customer_state>TN</customer_state>
                <customer_postal_code>37013</customer_postal_code>
                <customer_country>US</customer_country>
                <customer_phone>(123) 456-7890</customer_phone>
                <customer_email>[email protected]</customer_email>
                <customer_ip>71.228.237.177</customer_ip>
                <shipping_first_name>John</shipping_first_name>
                <shipping_last_name>Doe</shipping_last_name>
                <shipping_address1>1234 Any Street</shipping_address1>
                <shipping_address2></shipping_address2>
                <shipping_city>Some City</shipping_city>
                <shipping_state>TN</shipping_state>
                <shipping_postal_code>37013</shipping_postal_code>
                <shipping_country>US</shipping_country>
                <shipping_phone></shipping_phone>
                <shipping_service_description>UPS: Ground</shipping_service_description>
                <purchase_order></purchase_order>
                <product_total>20.00</product_total>
                <tax_total>0.00</tax_total>
                <shipping_total>4.38</shipping_total>
                <order_total>24.38</order_total>
                <order_total>24.38</order_total>
                <customer_password>1aab23051b24582c5dc8e23fc595d505</customer_password>
                <custom_fields>
                  <custom_field>
                    <custom_field_name>My_Cool_Text</custom_field_name>
                    <custom_field_value>Value123</custom_field_value>
                  </custom_field>
                  <custom_field>
                    <custom_field_name>Another_Custom_Field</custom_field_name>
                    <custom_field_value>10</custom_field_value>
                  </custom_field>
                </custom_fields>
                <transaction_details>
                  <transaction_detail>
                    <product_name>foo</product_name>
                    <product_price>20.00</product_price>
                    <product_quantity>1</product_quantity>
                    <product_weight>0.10</product_weight>
                    <product_code></product_code>
                    <subscription_frequency>1m</subscription_frequency>
                    <subscription_startdate>2007-07-07</subscription_startdate>
                    <next_transaction_date>2007-08-07</next_transaction_date>
                    <shipto>John Doe</shipto>
                    <category_description>Default for all products</category_description>
                    <category_code>DEFAULT</category_code>
                    <product_delivery_type>shipped</product_delivery_type>
                    <transaction_detail_options>
                      <transaction_detail_option>
                        <product_option_name>color</product_option_name>
                        <product_option_value>blue</product_option_value>
                        <price_mod></price_mod>
                        <weight_mod></weight_mod>
                      </transaction_detail_option>
                    </transaction_detail_options>
                  </transaction_detail>
                </transaction_details>
                <shipto_addresses>
                  <shipto_address>
                    <address_name>John Doe</address_name>
                    <shipto_first_name>John</shipto_first_name>
                    <shipto_last_name>Doe</shipto_last_name>
                    <shipto_address1>2345 Some Address</shipto_address1>
                    <shipto_address2></shipto_address2>
                    <shipto_city>Some City</shipto_city>
                    <shipto_state>TN</shipto_state>
                    <shipto_postal_code>37013</shipto_postal_code>
                    <shipto_country>US</shipto_country>
                    <shipto_shipping_service_description>DHL: Next Afternoon</shipto_shipping_service_description>
                    <shipto_subtotal>52.15</shipto_subtotal>
                    <shipto_tax_total>6.31</shipto_tax_total>
                    <shipto_shipping_total>15.76</shipto_shipping_total>
                    <shipto_total>74.22</shipto_total>
                    <shipto_custom_fields>
                      <shipto_custom_field>
                        <shipto_custom_field_name>My_Custom_Info</shipto_custom_field_name>
                        <shipto_custom_field_value>john's stuff</shipto_custom_field_value>
                      </shipto_custom_field>
                      <shipto_custom_field>
                        <shipto_custom_field_name>More_Custom_Info</shipto_custom_field_name>
                        <shipto_custom_field_value>more of john's stuff</shipto_custom_field_value>
                      </shipto_custom_field>
                    </shipto_custom_fields>
                  </shipto_address>
                </shipto_addresses>
              </transaction>
            </transactions>
          </foxydata>
          XML;
            $doc = new DOMDocument();
            $doc->loadXML($strXML);
            $trans = $doc->getElementsByTagName( "transaction" );
            foreach( $trans as $transaction )
            {
            //Order id
            $ids = $transaction->getElementsByTagName( "id" );
            $orderid = $ids->item(0)->nodeValue;
            //Transaction Date
            $transaction_dates = $transaction->getElementsByTagName( "transaction_date" );
            $transaction_date = $transaction_dates->item(0)->nodeValue;
            //Customer First Name
            $cust_firstnames = $transaction->getElementsByTagName( "customer_first_name" );
            $cust_firstname = $cust_firstnames->item(0)->nodeValue;
          
            
          //Insert statement
          if(!mysql_connect('host', 'username', 'password')) die("no connection to MySQL");
          if (!mysql_select_db('db66374_stageM')) die("couldn't select database");
          mysql_query("INSERT INTO  TABLE (id,trans_date,cust_firstname) VALUES ('".mysql_escape_string($orderid)."','".mysql_escape_string($transaction_date)."','".mysql_escape_string($cust_firstname)."')");
            }
            ?>
          

          I’ve been taking a longer look at the FoxyBack pages but can’t figure out classes to well.

          Based on the code above I’d hard code in the nodes that I need (cust info, transaction info, etc) then write it to a DB. Being that MODx takes an associative array for an INSERT statement I was just thinking I could loop through the whole thing, using the node names as column names and replace what potentially can be many many lines of code with just a few loops???
            I twitch because I care....and drink too much coffee.
            • 10487 MODX Staff
            • 1,535 Posts
            Garry Nutting Reply #5, 17 years ago
            Are you running PHP5? If you are, then you’d be much better off using SimpleXML. Anyhow, what kind of database structure have you adopted?
              Garry Nutting
              Senior Developer
              MODX, LLC

              Email: [email protected]
              Twitter: @garryn
              Web: modx.com
              • 26310
              • 130 Posts
              Yes I am running PHP5 with a MySQL DB. SimpleXML...I’ve read a little about it; will research more. Thanks for the advice!
                I twitch because I care....and drink too much coffee.
                • 4172
                • 5,888 Posts
                This function can also be usefull for parsing xml 2 array:

                http://www.bin-co.com/php/scripts/xml2array/
                  -------------------------------

                  you can buy me a beer, if you like MIGX

                  http://webcmsolutions.de/migx.html

                  Thanks!
                  • 26310
                  • 130 Posts
                  Using simpleXML I was able to take what I needed easily. Thanks everyone
                    I twitch because I care....and drink too much coffee.