Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader xml = new StringReader("<foo><bar>toast</bar></foo>");
    Foo foo = (Foo) unmarshaller.unmarshal(xml);

    System.out.println(foo.isBar());
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Response.class);

    Response response = new Response();
    response.setMessage("1 < 2");

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Root.class);

    Root root = new Root();
    root.setFoo("Hello\rWorld");
    root.setBar("Hello\nWorld");

    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(root, System.out);
}

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    Animal tiger = new Animal();
    tiger.setType("A");
    tiger.setValue("Tiger");

    JAXBContext jaxbContext = JAXBContext.newInstance(Animal.class);
    Marshaller m = jaxbContext.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(tiger, System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Employee.class);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Tom");
    object.setSalary(300);//from  w  w w  .j a  va2  s .c  o  m

    m.marshal(object, System.out);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Employee.class);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Employee object = new Employee();
    object.setCode("CA");
    object.setName("Cath");
    object.setSalary(300);/* w w w .  ja  v a2 s  .c om*/

    m.marshal(object, System.out);

    //to a file
    m.marshal(object, new FileOutputStream("result.xml"));

}

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(Card.class);
    context.createMarshaller().marshal(Card.CLUBS, System.out);
}

From source file:Main.java

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

    // Unmarshal #1 = Default Unmarshal
    System.out.println("Unmarshal #1");
    Root root = (Root) unmarshaller.unmarshal(new StringReader(XML));
    marshaller.marshal(root, System.out);

    // Unmarshal #2 - Override Default ValidationEventHandler
    System.out.println("Unmarshal #2");
    unmarshaller.setEventHandler(new ValidationEventHandler() {
        @Override/*from w  ww . ja v  a 2  s  .  c  om*/
        public boolean handleEvent(ValidationEvent event) {
            System.out.println(event.getMessage());
            return false;
        }
    });
    unmarshaller.unmarshal(new StringReader(XML));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Type.class);

    Type type = new Type();

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(Foo.class);

    Foo foo = new Foo();
    foo.setBar("Hello\nWorld");

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