⌛⌛ Country data (XML)¶
The submission consists of a Maven project. Place your answer into files pom.xml,
src/main/java/fi/tuni/prog3/round7/xmlcountries/CountryData.java in the directory
Round7/xmlcountries. Remember to pull task material from
As you could probably deduct from the above the task is submitted as a Maven project file and its
src under which the code files defined into the package
fi.tuni.prog3.round7.xmlcountries are found.
In this task you will try reading and writing XML data using a suitable library called jdom2: https://github.com/hunterhacker/jdom/wiki/JDOM2-A-Primer. The course material shows an example of a Maven project file that defines jdom2 as a library dependency.
The base data for the task consists of a sample from open data published by the world bank
containing the area, population and gross domestic product for a handful of countries. You need to
implement the classes
CountryData for processing this data so that they have
at least the following features. You can decide the rest of the details yourself.
Countrystores the name of the country (string), area (
double), population (
long) and gross domestic product (
Defined in the package
fi.tuni.prog3.round7.xmlcountries(i.e. set the line
package fi.tuni.prog3.round7.xmlcountries;into the very beginning of the class).
Implements the interface
Comparable<Country>so that the comparison is based on the name of the country according to the natural order of the
A public member function
String toString()that returns a String representation of the country in the form shown in the example outputs: first the name of the country, and after it the area, population and gross national product, each on separate lines and indented with two spaces.
Doublevalues with the precision of one decimal.
Public member functions
double getGdp()that return the data their names suggest.
CountryDataoffers two public static member functions for reading and writing country data in XML format.
Defined in the package
List<Country> readFromXmls(String areaFile, String populationFile, String gdpFile)reads country data from the XML files named by the parameters.
The files contain information about the area, population and gross domestic product of the countries as suggested by the file names. Note how the data is in three separate files.
Deduct the structure of the files by investigating the example input files. The structure of each file is identical: information for one country is in
fieldelements inside a
fieldelement has an attribute “name” that describes what information the element in question contains.
The function returns some kind of a list implementing the interface
List<Country>that contains the
Countryobjects depicting the read data. You must hence combine the data read from the three XML files into
void writeToXml(List<Country> countries, String countryFile)writes the information depicted by
Countryobjects in the list
countriesin XML format into the file named by the parameter
Has a root element
countryelement contains the information of one country in its child elements
Check the exact output format from the example outputs. They have been created using the
Format.getPrettyFormat()formatter of the jdom2 library.
The automatic tests (and the ones given below) assume that you make the following definitions
pom.xml project file:
The value of
The value of
A onejar plugin definition where the value of
CountryTestis the name of the given test class (described below).
There are three test sets available for the task. Below, the character
X refers to the number
of the test and is 1, 2 or 3.
You can test your classes with the test program given in the file
test files named in the form
gdpX.xml, the example
outputs named in the form
outputX.txt, and the example result files named in the form
CountryTest.java into the root of the
src/main/java subdirectory of your Maven project,
and the other files into the root directory of your Maven project (where the
pom.xml is). Note
CountryTest.java does not include a package definition and therefore is not placed into
a deeper subdirectory.
After this you can compile the program with
mvn package and run the test
java -jar target/xmlcountries-1.0.one-jar.jar areaX.xml populationX.xml gdpX.xml countriesX.xml.
The test number
X should produce the output depicted in the corresponding file
and create a file
countriesX.xml whose contents are identical with the file
A+ presents the exercise submission form here.