0

I'm hoping that someone can help me with what should be a simple thing, but I'm struggling a bit with it. I have an XML file with the following structure...

<Item MaintenanceType="C">
  <HazardousMaterialCode>N</HazardousMaterialCode>
  <ItemLevelGTIN GTINQualifier="UP">090127000380</ItemLevelGTIN>
  <PartNumber>0-1848-1</PartNumber>
  <BrandAAIAID>BBVL</BrandAAIAID>
  <BrandLabel>Holley</BrandLabel>
  <PartTerminologyID>5904</PartTerminologyID>
  <Descriptions>
    <Description MaintenanceType="C" DescriptionCode="DES" LanguageCode="EN">Street Carburetor</Description>
    <Description MaintenanceType="C" DescriptionCode="SHO" LanguageCode="EN">Crb</Description>
  </Descriptions>
  <Prices>
    <Pricing MaintenanceType="C" PriceType="JBR">
      <PriceSheetNumber>L30779-13</PriceSheetNumber>
      <CurrencyCode>USD</CurrencyCode>
      <EffectiveDate>2013-01-01</EffectiveDate>
      <Price UOM="PE">462.4600</Price>
    </Pricing>
    <Pricing MaintenanceType="C" PriceType="RET">
      <PriceSheetNumber>L30779-13</PriceSheetNumber>
      <CurrencyCode>USD</CurrencyCode>
      <EffectiveDate>2013-01-01</EffectiveDate>
      <Price UOM="PE">380.5500</Price>
    </Pricing>
    <Pricing MaintenanceType="C" PriceType="WD1">
      <PriceSheetNumber>L30779-13</PriceSheetNumber>
      <CurrencyCode>USD</CurrencyCode>
      <EffectiveDate>2013-01-01</EffectiveDate>
      <Price UOM="PE">314.4700</Price>
    </Pricing>
  </Prices>
  <ExtendedInformation>
    <ExtendedProductInformation MaintenanceType="C" EXPICode="CTO" LanguageCode="EN">US</ExtendedProductInformation>
    <ExtendedProductInformation MaintenanceType="C" EXPICode="NPC" LanguageCode="EN">A</ExtendedProductInformation>
    <ExtendedProductInformation MaintenanceType="C" EXPICode="HTS" LanguageCode="EN">8409914000</ExtendedProductInformation>
    <ExtendedProductInformation MaintenanceType="C" EXPICode="NAF" LanguageCode="EN">B</ExtendedProductInformation>
  </ExtendedInformation>
  <ProductAttributes>
    <ProductAttribute MaintenanceType="C" AttributeID="SKU" LanguageCode="EN">BBVL0-1848-1</ProductAttribute>
    <ProductAttribute MaintenanceType="C" AttributeID="ModDate" LanguageCode="EN">2012-12-31</ProductAttribute>
  </ProductAttributes>
  <Packages>
    <Package MaintenanceType="C">
      <PackageLevelGTIN>00090127000380</PackageLevelGTIN>
      <PackageUOM>EA</PackageUOM>
      <QuantityofEaches>1</QuantityofEaches>
      <Dimensions UOM="IN">
        <Height>7.5000</Height>
        <Width>11.0000</Width>
        <Length>12.2500</Length>
      </Dimensions>
      <Weights UOM="PG">
        <Weight>13.500</Weight>
        <DimensionalWeight>6.09</DimensionalWeight>
      </Weights>
    </Package>
  </Packages>
</Item>

I'm using the following PHP code to parser this gigantic XML file...

<?php
error_reporting(E_ALL);
ini_set( 'display_errors','1');
libxml_use_internal_errors(true);
libxml_clear_errors();

// create the reader object
$reader = new XMLReader();

// reader the XML file.
$reader->open('/home/hingeweb/www/tintworld/magmi/integration/scripts/worldpak-small.xml');

// Move to the first "[App name]" node in the file.
while ($reader->read() && $reader->name !== "App");
// Now that we're at the right depth, hop to the next "[App name]" until the end of tree/file.
while ($reader->name === "App") {
  $node = new SimpleXMLElement($reader->readOuterXML());

  // *** Do something of interest with $node, this is the App we have been looking for. ***
  // Skip to the next node of interest.
  $reader->next("App");
}

//foreach ($data as $key => $value) {
//  echo "Key: $key; Value: $value<br />\n";
//}

echo "Finished Processing... \n \n";

?>

I'm trying to set some of these results into an array. For example, I'd love to put $data->PartNumber into an array so that I can implode the results and import it into SQL. I don't want to import ALL of the fields of the XML file so I imagine I have to code these fields individually.

Is it possible someone could point me in the right direction for how to do this?

Thanks!

Brian Schroeter
  • 1,583
  • 5
  • 22
  • 41
  • see my answer to your other question. – michi Jun 19 '13 at 16:46
  • Your question already contains the answer: First of all sure this is possible and has been outlined in your previous question (I also added an example code). And then you write your own: *"I don't want to import ALL of the fields of the XML file so I imagine I have to code these fields individually."* Yes, you have to do that. E.g. put the field names into an array, obtain them then. For a [XMLReaderIterator](http://git.io/xmlreaderiterator) example there is a method called `XMLElementIterator::toArray()` to obtain from children: http://chat.stackoverflow.com/transcript/message/8300769#8300769 – hakre Jun 20 '13 at 08:57

0 Answers0