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.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Main {
    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Child.class);
        Child child = new Child();
        child.setParentProperty1("parentProperty1");
        child.setParentProperty2("parentProperty2");
        child.setChildProperty("childProperty");

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(child, System.out);
    }

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.PROPERTY)
    public static class Child extends Parent {
        private String childProperty;

        @Override
        @XmlElement
        public String getParentProperty2() {
            return super.getParentProperty2();
        }

        public String getChildProperty() {
            return childProperty;
        }

        public void setChildProperty(String childProperty) {
            this.childProperty = childProperty;
        }

    }

    @XmlAccessorType(XmlAccessType.NONE)
    public static class Parent {

        private String parentProperty1;
        private String parentProperty2;

        public String getParentProperty1() {
            return parentProperty1;
        }

        public void setParentProperty1(String parentProperty1) {
            this.parentProperty1 = parentProperty1;
        }

        public String getParentProperty2() {
            return parentProperty2;
        }

        public void setParentProperty2(String parentProperty2) {
            this.parentProperty2 = parentProperty2;
        }
    }
}