Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

public class Main {
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Note.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(new Note(), System.out);
    }

    @XmlRootElement
    @XmlType(propOrder = { "to", "from", "header", "body", "assignee" })
    public static class Note {
        private String to;
        private String from;
        private String header;
        private String body;
        private Assignee assignee;

        @XmlElement(nillable = true)
        public String getTo() {
            return to;
        }

        public void setTo(String to) {
            this.to = to;
        }

        @XmlElement(nillable = true)
        public String getFrom() {
            return from;
        }

        public void setFrom(String from) {
            this.from = from;
        }

        @XmlElement(nillable = true)
        public String getHeader() {
            return header;
        }

        public void setHeader(String header) {
            this.header = header;
        }

        @XmlElement(nillable = true)
        public String getBody() {
            return body;
        }

        public void setBody(String body) {
            this.body = body;
        }

        public Assignee getAssignee() {
            return assignee;
        }

        public void setAssignee(Assignee assignee) {
            this.assignee = assignee;
        }

    }

    @XmlJavaTypeAdapter(AssigneeAdapter.class)
    public static class Assignee {

        private String name;
        private String surname;

        @XmlElement(nillable = true)
        public String getName() {
            return name;
        }

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

        @XmlElement(nillable = true)
        public String getSurname() {
            return surname;
        }

        public void setSurname(String surname) {
            this.surname = surname;
        }

        public boolean isNull() {
            return null == name && null == surname;

        }
    }

    public static class AssigneeAdapter extends XmlAdapter<Assignee, Assignee> {

        @Override
        public Assignee unmarshal(Assignee v) throws Exception {
            if (v.isNull()) {
                return null;
            }
            return v;
        }

        @Override
        public Assignee marshal(Assignee v) throws Exception {
            if (null == v) {
                return new Assignee();
            }
            return v;
        }

    }

}