<?php $dataname = "e2fi1.csv"; $fp = fopen($dataname, "r"); //first line of csv is header, needs to be read out seperately //fgetcsv macht automatisch weiter bei der nächsten Zeile, merkt sich wo der pointer steht $row_header = fgetcsv($fp, null, ";"); $row_header_nummer = $row_header[0]; $row_header_vorname = $row_header[1]; $row_header_nachname = $row_header[2]; //create XML $xml_doc = new DOMDocument('1.0', 'utf-8'); $xml_doc->formatOutput = true; //build XML file with root element $xml_root = $xml_doc->createElement('klasse_e2fi1'); $xml_doc->appendChild($xml_root); //create string from CSV while (!feof($fp)) { $row_a = fgetcsv($fp, null, ";"); if (empty($row_a)) { continue; } $schueler_nummer = $row_a[0]; $schueler_vorname = $row_a[1]; $schueler_nachname = $row_a[2]; //create sub element for students $xml_student = $xml_doc->createElement('schueler'); $xml_student->setAttribute("nr", $schueler_nummer); $xml_root->appendChild($xml_student); //create element that fills the $xml_vorname = $xml_doc->createElement($row_header_vorname, $schueler_vorname); $xml_student->appendChild($xml_vorname); $xml_nachname = $xml_doc->createElement($row_header_nachname, $schueler_nachname); $xml_student->appendChild($xml_nachname); } $xml_filename = "klasse1fi1.xml"; $xml_doc->save($xml_filename);
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter