2018-11-10 15:22:45 +00:00
|
|
|
<?php
|
2018-11-10 16:47:10 +00:00
|
|
|
class WorldDataParser {
|
|
|
|
|
|
|
|
function parseCSV($path) {
|
|
|
|
$res = [];
|
|
|
|
|
|
|
|
if (!is_file($path)) {
|
|
|
|
echo 'Datei nicht vorhanden! - ' . $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
$handle = fopen($path, "r");
|
|
|
|
if ($handle !== FALSE) {
|
|
|
|
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
|
|
|
|
$res[] = $data; // data array (aktueller Zeile) zu multidimensionalen array hinzufügen
|
|
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
} else {
|
|
|
|
echo 'Konnte Datei nicht öffnen! - ' . $path;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $res;
|
|
|
|
}
|
2018-11-10 18:09:33 +00:00
|
|
|
|
|
|
|
function saveXML($data) {
|
2018-11-12 15:14:34 +00:00
|
|
|
$file = 'world_data.xml';
|
|
|
|
|
|
|
|
if (file_exists($file)) {
|
|
|
|
unlink($file);
|
2018-11-10 18:09:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$xml = '<?xml version="1.0" encoding="UTF-8"?>' . PHP_EOL;
|
|
|
|
$xml .= '<Countries>'. PHP_EOL;
|
|
|
|
foreach ($data as $zeilennummer => $csvzeile) {
|
|
|
|
if ($zeilennummer === 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$xml .= " <Country>". PHP_EOL;
|
|
|
|
foreach ($csvzeile as $spaltennummer => $wert) {
|
|
|
|
$spaltenname = $data[0][$spaltennummer];
|
|
|
|
$spaltenname = trim($spaltenname);
|
|
|
|
$wert = trim($wert);
|
|
|
|
$spaltenname = str_replace(" ", "_", $spaltenname);
|
|
|
|
|
|
|
|
$xml .= " <". $spaltenname .">";
|
|
|
|
$xml .= $wert;
|
|
|
|
$xml .= "</". $spaltenname .">". PHP_EOL;
|
|
|
|
}
|
|
|
|
$xml .= " </Country>". PHP_EOL;
|
|
|
|
}
|
|
|
|
$xml .= '</Countries>'. PHP_EOL;
|
|
|
|
|
|
|
|
file_put_contents($file, $xml);
|
|
|
|
|
2018-11-12 15:14:34 +00:00
|
|
|
return file_exists($file);
|
|
|
|
}
|
|
|
|
|
2018-11-12 19:53:11 +00:00
|
|
|
function read_file($path) {
|
|
|
|
$fh = fopen($path, "rb");
|
|
|
|
$data = fread($fh, filesize($path));
|
|
|
|
fclose($fh);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
2018-11-12 15:14:34 +00:00
|
|
|
function printXML($xml_file, $xslt_sheet) {
|
|
|
|
$xsl_doc = new DOMDocument();
|
2018-11-12 19:53:11 +00:00
|
|
|
if ($xsl_doc->loadXML($this->read_file($xslt_sheet)) === false) {
|
|
|
|
echo "reading xsl sheet failed";
|
2018-11-12 15:14:34 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$xml_doc = new DOMDocument();
|
2018-11-12 19:53:11 +00:00
|
|
|
if ($xml_doc->loadXML($this->read_file($xml_file)) === false) {
|
2018-11-12 15:14:34 +00:00
|
|
|
echo "reading world data xml failed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$xsl = new XSLTProcessor();
|
2018-11-10 18:09:33 +00:00
|
|
|
|
2018-11-12 15:14:34 +00:00
|
|
|
$xsl->importStyleSheet($xsl_doc);
|
|
|
|
echo $xsl->transformToXML($xml_doc);
|
2018-11-10 18:09:33 +00:00
|
|
|
}
|
2018-11-10 16:47:10 +00:00
|
|
|
}
|