PHP fputcsv通过XPath机箱问题

时间:2014-02-26 16:06:08

标签: php xml csv xpath fputcsv

我获取了很多xml数据并通过XPath解析它。 将数据写入文件时,除了第一个机箱外,我总是将机箱标签放在错误的位置。

以下是代码:

// define xpath expressions for the columns [] in v5.4+
        $columns = array('Description' => 'normalize-space(cac:Item/cbc:Description)',
          'SellersItemIdentification' => 'string(cac:Item/cac:SellersItemIdentification/cac:ID)',
          'StandardItemIdentification' => 'string(cac:Item/cac:StandardItemIdentification/cac:ID)',
          'ManufacturersItemIdentification' => 'normalize-space(cac:Item/cac:ManufacturersItemIdentification/cac:ID)',
          'Producer' => 'normalize-space(cac:Item/cac:ManufacturersItemIdentification/cac:IssuerParty/cac:PartyName/cbc:Name)',
          'PriceAmount' => 'string(cac:Item/cac:BasePrice/cbc:PriceAmount)',
          'BaseQuantity' => 'string(cac:Item/cac:BasePrice/cbc:BaseQuantity)',
          'Availability' => 'string(vco:Availability/vco:Code)');

        // open a file stream for the standard output
        if($startIndex == 0) {  
            //$csvStream = fopen('php://output', 'w');
            $saveStream = fopen('import/'.$filename, 'w');

            // write the columns names
            fputcsv($saveStream , array_keys($columns), ';', '^');
        } else {
            //$csvStream = fopen('php://output', 'a');
            $saveStream = fopen('import/'.$filename, 'a');
        }    
        //iterate all items
        foreach ($xpath->evaluate('//vco:ItemDetail') as $item) {
          // a row for an item
          $row = array();
          foreach ($columns as $expression) {
            // use the expression to fetch data for the columns and add it
            $row[] = $xpath->evaluate($expression, $item);
          }

          // write to the output stream
          fputcsv($saveStream, $row, ';', '^');
        }
        $startIndex = $startIndex + 500;
        $i++;

输出总是如下:

^NAME^;39570934;0348039;....

所以,问题是,正如你所看到的那样,第二个神秘的外壳让我有点疯狂。

希望你能阻止我

0 个答案:

没有答案