Aufgabe 2: eingelesene Datenstruktur als XML speichern + gitignore

This commit is contained in:
Selina 2018-11-10 19:09:33 +01:00
parent 15a51852f7
commit 06b5973624
4 changed files with 53 additions and 7 deletions

1
aufgabe2/.gitignore vendored Normal file
View file

@ -0,0 +1 @@
world_data.xml

View file

@ -1,4 +1,4 @@
ID ,Country ,birth rate per 1000,cell phones per 100,children per woman,electricity consumption per capita,gdp per_capita,gdp_ per_ capita_ growth,inflation annual,internet user per 100,life expectancy,military expenditure percent of gdp,gps_lat ,gps_long
id ,name ,birth rate per 1000,cell phones per 100,children per woman,electricity consumption per capita,gdp per_capita,gdp_ per_ capita_ growth,inflation annual,internet user per 100,life expectancy,military expenditure percent of gdp,gps_lat ,gps_long
001,Brazil ,16.405 ,90.01936334 ,1.862 ,2201.808724 ,4424.758692 ,-1.520402823 ,8.228535058 ,39.22 ,74 ,1.615173655 ,-14.235004000,-51.925280000
002,Canada ,10.625 ,70.70997244 ,1.668 ,15119.76414 ,25069.86915 ,-3.953353186 ,2.944408564 ,80.17086651 ,80.9 ,1.415710422 ,56.130366000 ,-106.346771000
003,Chile ,15.04 ,97.01862561 ,1.873 ,3276.06449 ,6451.631126 ,-2.610485847 ,7.47050527 ,38.8 ,78.8 ,3.064076139 ,-35.675147000,71.542969000

1 ID id Country name birth rate per 1000 cell phones per 100 children per woman electricity consumption per capita gdp per_capita gdp_ per_ capita_ growth inflation annual internet user per 100 life expectancy military expenditure percent of gdp gps_lat gps_long
2 001 Brazil 16.405 90.01936334 1.862 2201.808724 4424.758692 -1.520402823 8.228535058 39.22 74 1.615173655 -14.235004000 -51.925280000
3 002 Canada 10.625 70.70997244 1.668 15119.76414 25069.86915 -3.953353186 2.944408564 80.17086651 80.9 1.415710422 56.130366000 -106.346771000
4 003 Chile 15.04 97.01862561 1.873 3276.06449 6451.631126 -2.610485847 7.47050527 38.8 78.8 3.064076139 -35.675147000 71.542969000

View file

@ -1,7 +1,18 @@
<?php
/**
* Created by PhpStorm.
* User: Selin
* Date: 10.11.2018
* Time: 16:00
*/
require 'world_data_parser.php';
$parser = new WorldDataParser();
$path = 'resources/world_data_v1.csv';
$data = $parser->parseCSV($path);
$save = $parser->saveXML($data);
if ($save) {
echo "Erfolg";
}
else {
echo "Leider nicht erfolgreich";
}

View file

@ -20,4 +20,38 @@ class WorldDataParser {
return $res;
}
function saveXML($data) {
if (file_exists("world_data.xml")) {
unlink("world_data.xml");
}
$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 = 'world_data.xml';
file_put_contents($file, $xml);
return file_exists("world_data.xml");
}
}