简体   繁体   中英

using php xmlreader to get data from xml to mysql

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;'>";
   }    
}
?>

I think this might do what you want, or at least give you some ideas on how to progress - I mocked up some additional entries in the XML to show how it deals with various issues that might come up.

Note the comment that points out that, due to limitations of codepad, I didn't use the ideal string escaping function.

I am no expert in XML manipulation in PHP but I doubt your code using DOM and simpleXML both with xmlReader. So, I thought to check what I can suggest to you. I got this code and this looks straight to me. I suggest you concentrate on this for betterment. The is using DOM after XMLReader and after that it is using DOM's XPath as you are also doing.

<?php

// Parsing a large document with XMLReader with Expand - DOM/DOMXpath 
$reader = new XMLReader();

$reader->open("tooBig.xml");

while ($reader->read()) {
    switch ($reader->nodeType) {
        case (XMLREADER::ELEMENT):
        if ($reader->localName == "entry") {
            if ($reader->getAttribute("ID") == 5225) {
                $node = $reader->expand();
                $dom = new DomDocument();
                $n = $dom->importNode($node,true);
                $dom->appendChild($n);
                $xp = new DomXpath($dom);
                $res = $xp->query("/entry/title");
                echo $res->item(0)->nodeValue;
            }
        }
    }
}

?>

For more .

Can you check SimpleXMLElement ? it's good alternative

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM