From 06b59736242d3334aeb6c0996c63b0d8b22c80ad Mon Sep 17 00:00:00 2001 From: Selina Date: Sat, 10 Nov 2018 19:09:33 +0100 Subject: [PATCH] Aufgabe 2: eingelesene Datenstruktur als XML speichern + gitignore --- aufgabe2/.gitignore | 1 + aufgabe2/resources/world_data_v1.csv | 2 +- aufgabe2/save.inc.php | 23 ++++++++++++++----- aufgabe2/world_data_parser.php | 34 ++++++++++++++++++++++++++++ 4 files changed, 53 insertions(+), 7 deletions(-) create mode 100644 aufgabe2/.gitignore diff --git a/aufgabe2/.gitignore b/aufgabe2/.gitignore new file mode 100644 index 0000000..04f88b1 --- /dev/null +++ b/aufgabe2/.gitignore @@ -0,0 +1 @@ +world_data.xml diff --git a/aufgabe2/resources/world_data_v1.csv b/aufgabe2/resources/world_data_v1.csv index 29fa742..7d740b9 100644 --- a/aufgabe2/resources/world_data_v1.csv +++ b/aufgabe2/resources/world_data_v1.csv @@ -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 diff --git a/aufgabe2/save.inc.php b/aufgabe2/save.inc.php index ffaf151..78d6e94 100644 --- a/aufgabe2/save.inc.php +++ b/aufgabe2/save.inc.php @@ -1,7 +1,18 @@ parseCSV($path); + +$save = $parser->saveXML($data); + + +if ($save) { + echo "Erfolg"; +} +else { + echo "Leider nicht erfolgreich"; +} diff --git a/aufgabe2/world_data_parser.php b/aufgabe2/world_data_parser.php index 9d5596f..c4e7224 100644 --- a/aufgabe2/world_data_parser.php +++ b/aufgabe2/world_data_parser.php @@ -20,4 +20,38 @@ class WorldDataParser { return $res; } + + function saveXML($data) { + if (file_exists("world_data.xml")) { + unlink("world_data.xml"); + } + + $xml = '' . PHP_EOL; + $xml .= ''. PHP_EOL; + foreach ($data as $zeilennummer => $csvzeile) { + if ($zeilennummer === 0) { + continue; + } + + $xml .= " ". 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 .= "". PHP_EOL; + } + $xml .= " ". PHP_EOL; + } + $xml .= ''. PHP_EOL; + + $file = 'world_data.xml'; + file_put_contents($file, $xml); + + return file_exists("world_data.xml"); + + } } \ No newline at end of file