我获取大量 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;....
所以,正如你所看到的,问题是第二个神秘的围场,这让我有点疯狂。
希望你能阻止我