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.XmlTransient;
import javax.xml.bind.annotation.XmlType;

public class Main {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(BarX.class, BarY.class);
        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        BarX barX = new BarX();
        barX.setXThing("XThing");
        marshaller.marshal(barX, System.out);

        BarY barY = new BarY();
        barY.setYBlah("YBlah");
        marshaller.marshal(barY, System.out);
    }

    @XmlRootElement
    @XmlType(propOrder = { "YBlah" })
    public static class BarY extends Foo {

        @XmlElement(name = "blah")
        public String getYBlah() {
            return name;
        }

        public void setYBlah(String blah) {
            name = blah;
        }

    }

    @XmlTransient
    public static abstract class Foo {
        protected String name;
    }

    @XmlRootElement
    @XmlType(propOrder = { "XThing" })
    public static class BarX extends Foo {

        @XmlElement(name = "thing")
        public String getXThing() {
            return name;
        }

        public void setXThing(String thing) {
            name = thing;
        }

    }
}