Jump to content

Parsing XML data file in php


coolwinter

Recommended Posts

Hi,
I have written a php routine which parses a xml file and puts the data in the database the script goes into a certain folder looks for .xml files and then parses them one by one and does the database entry.
I have written this code so far however I am not able to get the right sql queries .
Each xml file will have one order id and several images associated with that orderid and there maybe several such xml files. Now for each orderid I have to insert the order id and imagenames and other data into the database.
HERE IS MY CODE.
[code]<?php
include("/home/caskie/www/demos/laserlight/includes/global_vars.php");
require_once("/home/caskie/www/demos/laserlight/includes/cls_db.php");
$db=new db(_DB_HOST_,_DB_NAME_,_DB_USER_NAME_,_DB_USER_PASSWORD_,true);
                $db->db_connect();
                $db->db_select_db();

$newDirectory = "/home/caskie/www/demos/laserlight/samples/php/photo/";

$xml_orderid_key = "*ORDER*ORDERID";
$xml_ordernumber_key = "*ORDER*ORDERNUMBER";
$xml_custname_key = "*ORDER*CUSTOMER*NAME";
$xml_qty_key = "*ORDER*ITEMS*ITEM*QUANTITY";
$xml_img_key = "*ORDER*ITEMS*ITEM*IMAGES*IMAGE*NAME";
$xml_retailsku_key = "*ORDER*ITEMS*ITEM*PRODUCT*RETAILSKU";
$xml_coaddress1_key = "*ORDER*SHIPTOADDRESS*LINE1";
$xml_coaddress2_key = "*ORDER*SHIPTOADDRESS*LINE2";
$xml_cocity_key = "*ORDER*SHIPTOADDRESS*CITY";
$xml_costate_key = "*ORDER*SHIPTOADDRESS*STATE";
$xml_cozipcode_key = "*ORDER*SHIPTOADDRESS*ZIPCODE";


$story_array = array();

$counter = 0;
class xml_story{
    var $orderid, $ordernumber, $custname, $qty, $imgname, $retailsku, $coaddress1, $coaddress2, $cocity, $costate, $cozipcode;
}

function startTag($parser, $data){
    global $current_tag;
    $current_tag .= "*$data";
    //echo "<BR>Start Tag = " . $current_tag."<BR>";
}

function endTag($parser, $data){
    global $current_tag;
    $tag_key = strrpos($current_tag, '*');
    $current_tag = substr($current_tag, 0, $tag_key);
    //echo "<BR>End Tag =" . $current_tag."<BR>";
}

function contents($parser, $data){
    global $current_tag, $xml_orderid_key, $xml_ordernumber_key, $xml_custname_key, $xml_qty_key, $xml_img_key, $xml_retailsku_key, $xml_coaddress1_key, $xml_coaddress2_key, $xml_cocity_key, $xml_costate_key, $xml_cozipcode_key, $counter, $story_array;

    switch($current_tag){
        case $xml_orderid_key:
            $story_array[$counter] = new xml_story();
            $story_array[$counter]->ORDERID = $data;
            break;
        case $xml_ordernumber_key:
            $story_array[$counter]->ORDERNUMBER = $data;
            break;
        case $xml_custname_key:
          $story_array[$counter]->NAME = $data;
          break;
        case $xml_qty_key:
            $story_array[$counter]->QUANTITY = $data;
            break;
        case $xml_cozipcode_key:
            $story_array[$counter]->ZIPCODE = $data;
              break;
        case $xml_retailsku_key:
            $story_array[$counter]->RETAILSKU = $data;
            break;
        case $xml_coaddress1_key:
            $story_array[$counter]->LINE1 = $data;
            break;
        case $xml_coaddress2_key:
            $story_array[$counter]->LINE2 = $data;
            break;
        case $xml_cocity_key:
            $story_array[$counter]->CITY = $data;
            break;
        case $xml_costate_key:
            $story_array[$counter]->STATE = $data;
            break;
        case $xml_img_key:
            $story_array[$counter]->NAME = $data;
            $counter++;
            break;
    }
}
foreach (glob('*.xml') as $filename)
{
    //unset($data);
    $xml_parser = xml_parser_create();

    xml_set_element_handler($xml_parser, "startTag", "endTag");

    xml_set_character_data_handler($xml_parser, "contents");

    $fp = fopen($filename, "r") or die("Could not open file");

    $data = fread($fp, filesize($filename)) or die("Could not read file");


    if(!(xml_parse($xml_parser, $data, feof($fp))))
        {
            die("Error on line " . xml_get_current_line_number($xml_parser));

           
        }

xml_parser_free($xml_parser);
    fclose($fp);
    //echo "<BR>File Opened = ".$filename."<BR>".$data."<BR>";
    $newfile = str_replace("xml", "ack", $filename);
    $cmd = "mv ".$filename." ".$newfile;
    //echo "Command to be executed". $cmd;
    system($cmd);
}
?>

    <html>
    <head>
    <title>Parsed Order Details </title>
    </head>
    <body bgcolor="#FFFFFF">
    <?php
        //echo "Start Before";
        //for($l=0;$l<count($Image_Name);$l++)
        //        echo "<BR>".$Image_Name[$l]."<BR>";
      //echo "End Before<hr><BR>";
     
        unset($Image_Name);
        //unset($Order_ID);
        unset($Order_Number);
        unset($Customer_Name);
        unset($Quantity);
        unset($RetailSku);
        unset($Company_Addr1);
        unset($Company_Addr2);
        unset($Company_City);
        unset($Company_Zipcode);
            //echo "Image = " . $Image_Name[0];
        for($x=0;$x<count($story_array);$x++)
            {
                if($story_array[$x]->ORDERID != '')
                $Order_ID[] = $story_array[$x]->ORDERID;
                $Order_Number[]=$story_array[$x]->ORDERNUMBER;
                $Customer_Name[] =$story_array[$x]->NAME;
                $Quantity[] = $story_array[$x]->QUANTITY;
                $Image_Name[] = $story_array[$x]->NAME;
                $RetailSku[] = $story_array[$x]->RETAILSKU;
                $Company_Addr1[] = $story_array[$x]->LINE1;
                $Company_Addr2[] = $story_array[$x]->LINE2;
                $Company_City[] = $story_array[$x]->CITY;
                $Company_State[] = $story_array[$x]->STATE;
                $Company_Zipcode[] = $story_array[$x]->ZIPCODE;
   
            }
            //echo "<br>Start after";
            //for($l=0;$l<count($Image_Name);$l++)
    //{
               
    //            echo "<BR>".$Image_Name[$l]."<BR>";
    //           
    //}
    //echo "End after<br>";
    for($j=0;$j< count($Image_Name); $j++)
    {
        if($RetailSku[$j] == 'laso2rdpers')
        $Color = "Red";
        elseif($RetailSku[$j] == 'laso2rdperss')
        {
            $Color = "Red Sample";
            $special_inst = "SAMPLE ORDER";
        }
        elseif($RetailSku[$j] == 'laso2gdpers')
            $Color = "Gold";
        elseif($RetailSku[$j] == 'laso2gdperss')
        {
            $Color = "Gold Sample";
            $special_inst = "SAMPLE ORDER";
        }
    $sql = "insert into tbl_cart (order_id, final_image, prod_type, Quantity, Color, Size, special_instructions, cust_id) values('".$Order_ID[0]."', '". $Image_Name[$j]."', 'Photo_Images', '".$Quantity[$j]."', '".$Color."', '3 1/4', '".$special_inst."', '')";
      echo "<BR>".$sql."<BR>";
    //unset($story_array); 
    $movefiles =  "mv ".$Image_Name[$j]." ".$newDirectory;
    $newfile = str_replace("xml", "ack", $filename);
    $cmd = "mv ".$filename." ".$newfile;
    //echo "Command to be executed". $cmd;
    //system($cmd);
    //unset($Image_Name);

    }//End for loop
unset ($data);
//unset($Image_Name);
?>


</body>
</html>
<?
//}//end foreach loop
//unset($Image_Name)
?>[/code]

However my arrays are not being initialized properly and so I get the values from my previous file when I parse the next file and so duplicate enteries are being made into the database.
Appreciate if anybody can help me in this.
The sample xml files are .
[code]<order>
        <version>2.1</version>
        <asnrequired>yes</asnrequired>
        <orderid>WAL-2222-2222-222222</orderid>
        <ordernumber>12345678987</ordernumber>
        <envelopenumber>456789</envelopenumber>
        <ordertype>Photo Gifts</ordertype>
        <entrydate>3/3/2004</entrydate>
        <orderdate>3/3/2004</orderdate>
        <printsreceived>Y</printsreceived>
        <expedite>0</expedite>
        <orderprice>42.00</orderprice>
        <redo>
            <originalorderId></originalorderId>
            <responsibility></responsibility>
            <notes></notes>   
        </redo>
        <customer>
            <name>Smith</name>
            <phone>555-555-5555</phone>
        </customer>
        <shipping>
            <carrier>Airborne</carrier>
            <carrierid>12</carrierid>
            <method>2 Day</method>
            <methodid>34</methodid>
        </shipping>
        <retailer>
            <retailerid>WAL</retailerid>       
            <retailername>Walgreens</retailername>
        </retailer>
        <store>
            <storeid>8765</storeid>
            <storename>Wilsons Super Store</storename>
        </store>
        <timestamp>11/23/03 11:31:03</timestamp>
        <duedate>12/11/2003</duedate>
        <fromaddress>
            <line1>PhotoTLC, Inc</line1>
            <line2>100 Tamal Plaza Suite 250</line2>
            <line3></line3>
            <city>Corte Madera</city>
            <county></county>
            <province></province>
            <state>CA</state>
            <zipcode>94925</zipcode>
            <country>US</country>
            <phone>555-555-5555</phone>
        </fromaddress>
        <toaddress>
            <line1>Walgreens ? Photo Dept</line1>
            <line2>Store No. 167</line2>
            <line3>Luna St  Carro St</line3>
            <city>San German</city>
            <county></county>
            <province></province>
            <state>PR</state>
            <zipcode>06823</zipcode>
            <country>US</country>
            <phone>555-555-5555</phone>
        </toaddress>
        <shiptoaddress>
            <line1>12 Mall Drive</line1>
            <line2></line2>
            <line3></line3>
            <city>Millway</city>
            <county></county>
            <province></province>
            <state>MI</state>
            <zipcode>44444</zipcode>
            <country>US</country>
                    <phone>555-555-5555</phone>
        </shiptoaddress>
        <region>12</region>
        <mailroom>222</mailroom>   
        <itemcount>1</itemcount>
        <items>
            <item>
                <itemsequence>1</itemsequence>
                <product>
                    <description>Large dual print T-Shirt w/Text</description>
                    <retailsku>P675656</retailsku>
                    <fulfillmentsku>333444555</fulfillmentsku>
                    <wholesaleprice>17.99</wholesaleprice>
                    <retailprice>29.95</retailprice>
                </product>
                <discount>
                    <reason>$5 off coupon</reason>
                    <amount>5</amount>
                </discount>
                <quantity>1</quantity>
                <comment></comment>
                <imagecount>2</imagecount>
                <images>
                    <image>
                        <description>front image</description>
                        <name>WAL-2345-7665544323-04112003-01.jpg</name>
                        <url></url>
                    </image>
                    <image>
                        <description>back image</description>
                        <name>WAL-2345-7665544323-04112003-01.jpg</name>
                        <url></url>
                    </image>
                </images>
                <itemattributecount>2</itemattributecount>
                <itemattributes>
                    <itemattribute>
                        <name>FrontText</name>
                        <value>Hello</value>
                    </itemattribute>
                    <itemattribute>
                        <name>BackText</name>
                        <value>Goodbye</value>
                    </itemattribute>
                </itemattributes>
            </item>
        </items>
</order>[/code]

[code]<order>
  <version>2.1</version>
  <asnrequired>no</asnrequired>
  <orderid>13238905</orderid>
  <ordernumber>13238905</ordernumber>
  <envelopenumber>071906</envelopenumber>
  <ordertype>Photo Gifts</ordertype>
  <entrydate>07/19/2006</entrydate>
  <orderdate>07/19/2006</orderdate>
  <printsreceived>N</printsreceived>
  <expedite>0</expedite>
  <orderprice>0.00</orderprice>
  <redo>
    <originalorderId></originalorderId>
    <responsibility></responsibility>
    <notes></notes>
  </redo>
  <customer>
    <name>walgreens samples</name>
    <phone>315-704-0290</phone>
  </customer>
  <shipping>
    <carrier></carrier>
    <carrierid></carrierid>
    <method></method>
    <methodid></methodid>
  </shipping>
  <retailer>
    <retailerid>USW</retailerid>
    <retailername>Walgreens</retailername>
  </retailer>
  <store>
    <storeid>10157</storeid>
    <storename>Walgreens</storename>
  </store>
  <timestamp>07/25/2006 17:02:03</timestamp>
  <duedate>08/04/2006</duedate>
  <fromaddress>
    <line1>PhotoTLC, Inc.</line1>
    <line2>3925 Cypress Drive </line2>
    <line3></line3>
    <city>Petaluma</city>
    <county></county>
    <province></province>
    <state>CA</state>
    <zipcode>94954</zipcode>
    <country>US</country>
    <phone>1-888-898-1901</phone>
  </fromaddress>
  <toaddress>
    <line1>WALGREENS - PHOTO DEPT</line1>
    <line2>Store No. 10157</line2>
    <line3>150 Grant Ave </line3>
    <city>Auburn</city>
    <county></county>
    <province></province>
    <state>NY</state>
    <zipcode>13021</zipcode>
    <country>US</country>
    <phone>315-704-0290</phone>
  </toaddress>
  <shiptoaddress>
    <line1>WALGREENS - PHOTO DEPT</line1>
    <line2>Store No. 10157</line2>
    <line3>150 Grant Ave </line3>
    <city>Auburn</city>
    <county></county>
    <province></province>
    <state>NY</state>
    <zipcode>13021</zipcode>
    <country>US</country>
    <phone>315-704-0290</phone>
  </shiptoaddress>
  <region></region>
  <mailroom></mailroom>
  <itemcount>5</itemcount>
  <items>
    <item>
      <itemsequence>11</itemsequence>
      <product>
        <description>*Clock 12&quot; Sports MLB sample</description>
        <retailsku>NWS73000536</retailsku>
        <fulfillmentsku>4315</fulfillmentsku>
        <wholesaleprice>14.0000</wholesaleprice>
        <retailprice>0.0000</retailprice>
      </product>
      <discount>
        <reason></reason>
        <amount></amount>
      </discount>
      <quantity>1</quantity>
      <comment></comment>
      <imagecount>1</imagecount>
      <images>
        <image>
        <description></description>
        <name>USW071906-25.13238905.jpg</name>
        <url>http://images.phototlc.net/gifts/05/USW071906-25.13238905.jpg</url>
        </image>
      </images>
      <itemattributecount>1</itemattributecount>
      <itemattributes>
        <itemattribute>
          <name>Baseball Team</name>
          <value>FLORIDA MARLINS|14-31</value>
        </itemattribute>
      </itemattributes>
    </item>
    <item>
      <itemsequence>12</itemsequence>
      <product>
        <description>*Mousepad Sports NFL Sample</description>
        <retailsku>NWS80000544</retailsku>
        <fulfillmentsku>5620</fulfillmentsku>
        <wholesaleprice>5.8000</wholesaleprice>
        <retailprice>0.0000</retailprice>
      </product>
      <discount>
        <reason></reason>
        <amount></amount>
      </discount>
      <quantity>1</quantity>
      <comment></comment>
      <imagecount>1</imagecount>
      <images>
        <image>
        <description></description>
        <name>USW071906-26.13238905.jpg</name>
        <url>http://images.phototlc.net/gifts/05/USW071906-26.13238905.jpg</url>
        </image>
      </images>
      <itemattributecount>1</itemattributecount>
      <itemattributes>
        <itemattribute>
          <name>Football Team</name>
          <value>PHIL EAGLES|10-51</value>
        </itemattribute>
      </itemattributes>
    </item>
    <item>
      <itemsequence>13</itemsequence>
      <product>
        <description>*Mug 11oz. White Sports MLB Sample</description>
        <retailsku>NWS81000545</retailsku>
        <fulfillmentsku>2571</fulfillmentsku>
        <wholesaleprice>6.8000</wholesaleprice>
        <retailprice>0.0000</retailprice>
      </product>
      <discount>
        <reason></reason>
        <amount></amount>
      </discount>
      <quantity>1</quantity>
      <comment></comment>
      <imagecount>1</imagecount>
      <images>
        <image>
        <description></description>
        <name>USW071906-27.13238905.jpg</name>
        <url>http://images.phototlc.net/gifts/05/USW071906-27.13238905.jpg</url>
        </image>
      </images>
      <itemattributecount>1</itemattributecount>
      <itemattributes>
        <itemattribute>
          <name>Baseball Team</name>
          <value>MINNESOTA TWINS|14-41</value>
        </itemattribute>
      </itemattributes>
    </item>
    <item>
      <itemsequence>14</itemsequence>
      <product>
        <description>*Stein 16 oz Sports NFL, sample</description>
        <retailsku>NWS88000556</retailsku>
        <fulfillmentsku>5546</fulfillmentsku>
        <wholesaleprice>8.8000</wholesaleprice>
        <retailprice>0.0000</retailprice>
      </product>
      <discount>
        <reason></reason>
        <amount></amount>
      </discount>
      <quantity>1</quantity>
      <comment></comment>
      <imagecount>1</imagecount>
      <images>
        <image>
        <description></description>
        <name>USW071906-28.13238905.jpg</name>
        <url>http://images.phototlc.net/gifts/05/USW071906-28.13238905.jpg</url>
        </image>
      </images>
      <itemattributecount>1</itemattributecount>
      <itemattributes>
        <itemattribute>
          <name>Football Team</name>
          <value>SD CHARGERS|10-57</value>
        </itemattribute>
      </itemattributes>
    </item>
    <item>
      <itemsequence>15</itemsequence>
      <product>
        <description>*Tumbler 16 oz Slimline Sports MLB Sample</description>
        <retailsku>NWS89000560</retailsku>
        <fulfillmentsku>5544</fulfillmentsku>
        <wholesaleprice>8.0000</wholesaleprice>
        <retailprice>0.0000</retailprice>
      </product>
      <discount>
        <reason></reason>
        <amount></amount>
      </discount>
      <quantity>1</quantity>
      <comment></comment>
      <imagecount>1</imagecount>
      <images>
        <image>
        <description></description>
        <name>USW071906-29.13238905.jpg</name>
        <url>http://images.phototlc.net/gifts/05/USW071906-29.13238905.jpg</url>
        </image>
      </images>
      <itemattributecount>1</itemattributecount>
      <itemattributes>
        <itemattribute>
          <name>Baseball Team</name>
          <value>ATLANTA BRAVES|14-11</value>
        </itemattribute>
      </itemattributes>
    </item>
  </items>
</order>[/code]
Appreciate any help in this regards,
Thanks,
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.