0
所以我有一个类似于这样的XML文件:SimpleXML来CSV缺少标签
<container>
<example1>some text</example1>
<example2>some text</example2>
<example3>some text</example3>
<example4>some text</example4>
</container>
<container>
<example1>some text</example1>
<example2>some text</example2>
<example4>some text</example4>
</container>
基本上,如果example3
不包含任何信息的XML笔者决定彻底离开它。问题是,当我使用我的脚本转换为CSV时,由于第二个example4
中的文本显示在CSV文件的example3
的标题下,因此此特定的XML文件无法正确转换为CSV。
这是我必须正常使用其他XML文件的PHP脚本。
$file='input.xml';
if (file_exists($file)) {
$xml = simplexml_load_file($file);
$f = fopen('output.csv', 'w');
// array to hold the field names
$headers = array();
// loop through the first set of fields to get names
foreach ($xml->container->children() as $field) {
// put the field name into array
$headers[] = $field->getName();
}
// print headers to CSV
fputcsv($f, $headers, ',', '"');
foreach ($xml->container as $information) {
fputcsv($f, get_object_vars($information), ',', '"');
}
fclose($f);
}
我无法弄清楚如何预定义我需要的头和插入在CSV正确的列中的正确的信息。
任何指针非常赞赏。
感谢
迈克
嗨,辉煌!谢谢,工作过一种享受! –