views:

583

answers:

0

I am using php and xmlreader to retreive data from an xml file and insert into a mysql table. I chose xmlreader because the files supplied me are 500mb. I am new to all of this and am at a sticking point getting the data to insert properly into the mysql table.

Sample xml from file...

<us:ItemMaster>
  <us:ItemMasterHeader>
    <oa:ItemID agencyRole="Prefix_Number" >
      <oa:ID>CTY</oa:ID>
    </oa:ItemID>
    <oa:ItemID agencyRole="Stock_Number_Butted" >
      <oa:ID>TN2100</oa:ID>
    </oa:ItemID>
    <oa:Specification>
      <oa:Property sequence="3" >
        <oa:NameValue name="Color(s)" >Black</oa:NameValue>
      </oa:Property>
      <oa:Property sequence="22" >
        <oa:NameValue name="Coverage Percent " >5.00 %</oa:NameValue>
      </oa:Property>
    </oa:Specification>
  </us:ItemMasterHeader>
</us:ItemMaster>

I am reading the xml file using xmlreader and utilizing expand() to SimpleXML for flexibility in getting the particulars. I could not figure out how to do what I wanted using strictly xmlreader.

I want each record of the mysql table to reflect prefix, stockNumber, attributePriority, AttributeName and AttributeValue.

Here's my code so far...

<?php
$reader = XMLReader::open($file);

while ($reader->read())  {
  if ($reader->nodeType == XMLREADER::ELEMENT && 
     $reader->localName == 'ItemMasterHeader' )  {
    $node = $reader->expand();
    $dom = new DomDocument();
    $n = $dom->importNode($node,true);
    $dom->appendChild($n);
    $sxe = simplexml_import_dom($n);

    foreach ($sxe->xpath("//oa:Property[@sequence]") as $Property) {
      $AttributePriority = $Property[@sequence];
      echo "(" . $AttributePriority . ") ";

      $Prefix = $sxe->xpath("//oa:ItemID[@agencyRole = 'Prefix_Number']/oa:ID");
      foreach ($Prefix as $Prefix)  {
        echo $Prefix;
      }

      $StockNumber = $sxe->xpath("//oa:ItemID[@agencyRole        ='Stock_Number_Butted']/oa:ID");
          foreach ($StockNumber as $StockNumber)  {
        echo $StockNumber;
      }
   }
   foreach ($sxe->xpath("//oa:NameValue[@name]") as $NameValue)  {
     $AttributeName = $NameValue[@name];
     echo $AttributeName . " ";
   }
   foreach ($sxe->xpath("//oa:NameValue[@name]") as $NameValue)  {
     $AttributeValue = $NameValue;
     echo $AttributeValue . "<br/>";
   }

// mysql insert
mysql_query("INSERT INTO $table  (Prefix,StockNumber,AttributePriority,AttributeName,AttributeValue) 
VALUES('$Prefix','$StockNumber','$AttributePriority','$AttributeName','$AttributeValue')");    
}
  if($reader->nodeType == XMLREADER::ELEMENT && $reader->localName == 'ItemMaster')  {
    // visual seperator between products
    echo "<hr style = 'color:red;'>";
   }    
}
?>