package JAXBmondial;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import java.io.File;
import java.math.BigInteger;
import java.math.BigDecimal;
import java.util.List;

public class JAXBmondial {
public static void main (String args[]) {
 try {
  JAXBContext jc = JAXBContext.newInstance("JAXBmondial");
  Unmarshaller unmarshaller = jc.createUnmarshaller();
  Mondial mondial = (Mondial) unmarshaller.unmarshal(new File("mondial-jaxb.xml"));

  List<Country> countryList  = mondial.getCountry();
  for (Country country: countryList)
  {
    System.out.println("Country: " + country.getName()  );
    BigDecimal area = country.getArea();
    BigInteger countrypop = country.getPopulation().getValue();
      // datatype class handling is ugly (pop is BigInteger, area is BigDecimal):
    System.out.println("   pop: " +  countrypop + ", area: " + area + ", density: "
              + new BigDecimal(countrypop).divide(area, java.math.BigDecimal.ROUND_HALF_UP));


    // Java knows from the annotation of the IDREF attribute that this is a city:
    City cap = country.getCapital();
    System.out.println("   cap: " +  cap.getName());

    List<Province> provList = country.getProvince();
    for (Province prov: provList) {
      System.out.println("   Province name: " + prov.getName().trim());
      System.out.println("            area: " + prov.getArea().toString());
      System.out.println("            pop : " + prov.getPopulation().getValue());
      List<City> cityList = prov.getCity();
      for (City city : cityList) {
        System.out.println("            City name: "+city.getName().trim());
        List<Populationtype> poplist = city.getPopulation();
        for (Populationtype p : poplist)
          System.out.println("                 pop "
                               + p.getYear() + ": " + p.getValue());
  } } }
 } catch (Exception e ) { e.printStackTrace(); }
}}
