⌛⌛ Country data (XML)¶
The submission consists of a Maven project. Place your answer into files pom.xml,
src/main/java/fi/tuni/prog3/round8/xmlcountries/Country.java and
src/main/java/fi/tuni/prog3/round8/xmlcountries/CountryData.java in the directory
Round8/xmlcountries. Remember to pull task material from student_template_project
.
As you could probably deduct from the above the task is submitted as a Maven project file and its
subdirectory src
under which the code files defined into the package
fi.tuni.prog3.round8.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 Country
and CountryData
for processing this data so that they have
at least the following features. You can decide the rest of the details yourself.
Class
Country
stores the name of the country (string), area (double
), population (long
) and gross domestic product (double
).Defined in the package
fi.tuni.prog3.round8.xmlcountries
(i.e. set the linepackage fi.tuni.prog3.round8.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 theString
class.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.Double
values with the precision of one decimal.Public member functions
String getName()
,double getArea()
,long getPopulation()
anddouble getGdp()
that return the data their names suggest.
Class
CountryData
offers two public static member functions for reading and writing country data in XML format.Defined in the package
fi.tuni.prog3.round8.xmlcountries
.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
field
elements inside arecord
element. Eachfield
element 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 theCountry
objects depicting the read data. You must hence combine the data read from the three XML files intoCountry
objects.
void writeToXml(List<Country> countries, String countryFile)
writes the information depicted byCountry
objects in the listcountries
in XML format into the file named by the parametercountryFile
.Has a root element
countries
which containscountry
elements. Eachcountry
element contains the information of one country in its child elementsname
,area
,population
andgdp
.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
in your pom.xml
project file:
The value of
artifactId
isxmlcountries
.The value of
version
element is1.0
.A onejar plugin definition where the value of
mainClass
isCountryTest
.CountryTest
is the name of the given test class (described below).
Testing¶
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 CountryTest.java
, the
test files named in the form areaX.xml
, populationX.xml
and gdpX.xml
, the example
outputs named in the form outputX.txt
, and the example result files named in the form
resultX.xml
.
Set 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
that 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 X
as
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 outputX.txt
and create a file countriesX.xml
whose contents are identical with the file resultX.xml
.
A+ presents the exercise submission form here.