Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/226.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP将XML转换为CSV_Php_Xml_Converter - Fatal编程技术网

使用PHP将XML转换为CSV

使用PHP将XML转换为CSV,php,xml,converter,Php,Xml,Converter,我使用以下代码将XML文件转换为CSV格式。不幸的是,它似乎无法识别XML中的每个条目,因此XML文件最终为空 <?php $filexml='test.xml'; if (file_exists($filexml)) { echo 'File Exists'; $xml = simplexml_load_file($filexml); $f = fopen('test.csv', 'w'); foreach ($xml->Item as $item) {

我使用以下代码将XML文件转换为CSV格式。不幸的是,它似乎无法识别XML中的每个条目,因此XML文件最终为空

<?php
$filexml='test.xml';
if (file_exists($filexml)) {
  echo 'File Exists';
$xml = simplexml_load_file($filexml);
  $f = fopen('test.csv', 'w');
  foreach ($xml->Item as $item) {
        fputcsv($f, get_object_vars($item),',','"');
  }
  fclose($f);
}
?>
项目作为$Item){
fputcsv($f,get_object_vars($item),“,”,“,”);
}
外国法郎(f美元);
}
?>
下面是我的XML文件的一个示例

<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>

N
090127000380
0-1848-1
BBVL
霍利
5904
街道汽化器
Crb
L30779-13
美元
2013-01-01
462.4600
L30779-13
美元
2013-01-01
380.5500
L30779-13
美元
2013-01-01
314.4700
美国
A.
8409914000
B
BBVL0-1848-1
2012-12-31
00090127000380
每个
1.
7.5000
11
12.2500
13.500
6.09
知道我做错了什么吗?

试试这个

$filexml='test.xml';

    if (file_exists($filexml)) 
           {
       $xml = simplexml_load_file($filexml);
       $f = fopen('test.csv', 'w');
       createCsv($xml, $f);
       fclose($f);
    }

    function createCsv($xml,$f)
    {

        foreach ($xml->children() as $item) 
        {

           $hasChild = (count($item->children()) > 0)?true:false;

        if( ! $hasChild)
        {
           $put_arr = array($item->getName(),$item); 
           fputcsv($f, $put_arr ,',','"');

        }
        else
        {
         createCsv($item, $f);
        }
     }

    }

该文件可写吗?是的。我使用了一个更简单的示例,它成功了。:)您有没有收到任何错误?或不正确的csv文件?该文件根本没有填充。我没有收到任何错误。您的答案如下:)[在此处输入链接说明][1][1]:可以这样做,但每个节点都有列?我想获取一个xml并使用节点类型作为列标题。