title: Convert XML to CSV with PHP
body: <p>I'm using the following code to convert my XML file to a CSV format. Unfortunately, it seems to not be recognizing each entry in the XML and so the XML file ends up being blank.</p>
  
  <pre><code>&lt;?php
  $filexml='test.xml';
  if (file_exists($filexml)) {
    echo 'File Exists';
  $xml = simplexml_load_file($filexml);
    $f = fopen('test.csv', 'w');
    foreach ($xml-&gt;Item as $item) {
          fputcsv($f, get_object_vars($item),',','"');
    }
    fclose($f);
  }
  ?&gt;
  </code></pre>
  
  <p>An example of my XML file is below...</p>
  
  <pre><code>&lt;Item MaintenanceType="C"&gt;
    &lt;HazardousMaterialCode&gt;N&lt;/HazardousMaterialCode&gt;
    &lt;ItemLevelGTIN GTINQualifier="UP"&gt;090127000380&lt;/ItemLevelGTIN&gt;
    &lt;PartNumber&gt;0-1848-1&lt;/PartNumber&gt;
    &lt;BrandAAIAID&gt;BBVL&lt;/BrandAAIAID&gt;
    &lt;BrandLabel&gt;Holley&lt;/BrandLabel&gt;
    &lt;PartTerminologyID&gt;5904&lt;/PartTerminologyID&gt;
    &lt;Descriptions&gt;
      &lt;Description MaintenanceType="C" DescriptionCode="DES" LanguageCode="EN"&gt;Street Carburetor&lt;/Description&gt;
      &lt;Description MaintenanceType="C" DescriptionCode="SHO" LanguageCode="EN"&gt;Crb&lt;/Description&gt;
    &lt;/Descriptions&gt;
    &lt;Prices&gt;
      &lt;Pricing MaintenanceType="C" PriceType="JBR"&gt;
        &lt;PriceSheetNumber&gt;L30779-13&lt;/PriceSheetNumber&gt;
        &lt;CurrencyCode&gt;USD&lt;/CurrencyCode&gt;
        &lt;EffectiveDate&gt;2013-01-01&lt;/EffectiveDate&gt;
        &lt;Price UOM="PE"&gt;462.4600&lt;/Price&gt;
      &lt;/Pricing&gt;
      &lt;Pricing MaintenanceType="C" PriceType="RET"&gt;
        &lt;PriceSheetNumber&gt;L30779-13&lt;/PriceSheetNumber&gt;
        &lt;CurrencyCode&gt;USD&lt;/CurrencyCode&gt;
        &lt;EffectiveDate&gt;2013-01-01&lt;/EffectiveDate&gt;
        &lt;Price UOM="PE"&gt;380.5500&lt;/Price&gt;
      &lt;/Pricing&gt;
      &lt;Pricing MaintenanceType="C" PriceType="WD1"&gt;
        &lt;PriceSheetNumber&gt;L30779-13&lt;/PriceSheetNumber&gt;
        &lt;CurrencyCode&gt;USD&lt;/CurrencyCode&gt;
        &lt;EffectiveDate&gt;2013-01-01&lt;/EffectiveDate&gt;
        &lt;Price UOM="PE"&gt;314.4700&lt;/Price&gt;
      &lt;/Pricing&gt;
    &lt;/Prices&gt;
    &lt;ExtendedInformation&gt;
      &lt;ExtendedProductInformation MaintenanceType="C" EXPICode="CTO" LanguageCode="EN"&gt;US&lt;/ExtendedProductInformation&gt;
      &lt;ExtendedProductInformation MaintenanceType="C" EXPICode="NPC" LanguageCode="EN"&gt;A&lt;/ExtendedProductInformation&gt;
      &lt;ExtendedProductInformation MaintenanceType="C" EXPICode="HTS" LanguageCode="EN"&gt;8409914000&lt;/ExtendedProductInformation&gt;
      &lt;ExtendedProductInformation MaintenanceType="C" EXPICode="NAF" LanguageCode="EN"&gt;B&lt;/ExtendedProductInformation&gt;
    &lt;/ExtendedInformation&gt;
    &lt;ProductAttributes&gt;
      &lt;ProductAttribute MaintenanceType="C" AttributeID="SKU" LanguageCode="EN"&gt;BBVL0-1848-1&lt;/ProductAttribute&gt;
      &lt;ProductAttribute MaintenanceType="C" AttributeID="ModDate" LanguageCode="EN"&gt;2012-12-31&lt;/ProductAttribute&gt;
    &lt;/ProductAttributes&gt;
    &lt;Packages&gt;
      &lt;Package MaintenanceType="C"&gt;
        &lt;PackageLevelGTIN&gt;00090127000380&lt;/PackageLevelGTIN&gt;
        &lt;PackageUOM&gt;EA&lt;/PackageUOM&gt;
        &lt;QuantityofEaches&gt;1&lt;/QuantityofEaches&gt;
        &lt;Dimensions UOM="IN"&gt;
          &lt;Height&gt;7.5000&lt;/Height&gt;
          &lt;Width&gt;11.0000&lt;/Width&gt;
          &lt;Length&gt;12.2500&lt;/Length&gt;
        &lt;/Dimensions&gt;
        &lt;Weights UOM="PG"&gt;
          &lt;Weight&gt;13.500&lt;/Weight&gt;
          &lt;DimensionalWeight&gt;6.09&lt;/DimensionalWeight&gt;
        &lt;/Weights&gt;
      &lt;/Package&gt;
    &lt;/Packages&gt;
  &lt;/Item&gt;
  </code></pre>
  
  <p>Any ideas what I did wrong?</p>