package com.javahonk;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import com.javahonk.bean.Frame;
import com.javahonk.bean.Powertrain;
import com.javahonk.bean.Vehicle;
import com.javahonk.bean.Vehicles;
import com.javahonk.bean.Wheel;
import com.javahonk.bean.Wheels;

public class JavaToXML {

	public static void main(String[] args) {

		try {

			Vehicle vehicle = new Vehicle();
			vehicle.setId("Vehicle 1");

			Frame frame = new Frame();
			frame.setMaterial("Metal");
			vehicle.setFrame(frame);

			Powertrain powertrain = new Powertrain();
			powertrain.setHuman("Human");
			List<Powertrain> powertrainList = 
					new ArrayList<Powertrain>();
			powertrainList.add(powertrain);
			vehicle.setPowertrainsList(powertrainList);

			Wheels wheels = new Wheels();
			List<Wheels> wheelsList = new ArrayList<Wheels>();

			Wheel wheel = new Wheel();
			wheel.setMaterial("Metal");
			wheel.setPosition("right rear");
			List<Wheel> wheelList = new ArrayList<Wheel>();
			wheelList.add(wheel);
			wheels.setWheelsList(wheelList);
			wheelsList.add(wheels);
			vehicle.setWheelsList(wheelsList);

			Vehicles vehicles = new Vehicles();
			List<Vehicle> vehicleList = new ArrayList<Vehicle>();
			vehicleList.add(vehicle);
			vehicles.setVehiclesList(vehicleList);

			JAXBContext jaxbContext = JAXBContext
					.newInstance(Vehicles.class);
			Marshaller jaxbMarshaller = jaxbContext
					.createMarshaller();

			jaxbMarshaller.setProperty(Marshaller
					.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

			File XMLfile = new File("JavaToXMLVehicle");

			jaxbMarshaller.marshal(vehicles, XMLfile);
			jaxbMarshaller.marshal(vehicles, System.out);
			
		} catch (JAXBException e) {
			e.printStackTrace();
		}

	}

}
