Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.util.ArrayList;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;

public class Main {
    public static void main(String[] args) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Vehicals.class);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Vehicals vehicals = new Vehicals();

        List<Car> cars = new ArrayList<Car>();
        Car c = new Car();
        c.setName("Mercedes");
        cars.add(c);

        c = new Car();
        c.setName("BMW");
        cars.add(c);
        vehicals.setCar(cars);
        m.marshal(vehicals, System.out);
    }

    @XmlRootElement
    public static class Vehicals {

        private List<Car> Car;

        public List<Car> getCar() {
            return Car;
        }

        public void setCar(List<Car> cars) {
            this.Car = cars;
        }
    }

    @XmlRootElement
    public static class Car {

        @XmlTransient
        private Long id;

        private String name;

        @XmlTransient
        private String code;

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public String getCode() {
            return code;
        }

        public void setCode(String code) {
            this.code = code;
        }
    }
}