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正確的列中的正確的信息。
任何指針非常讚賞。
感謝
邁克
嗨,輝煌!謝謝,工作過一種享受! –