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(Main.class);

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    StringReader xml = new StringReader("<element>data</element>");
    Main myClass = (Main) unmarshaller.unmarshal(xml);

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

From source file:PersonUnmarshaller.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("person");
    Unmarshaller unmarshaller = context.createUnmarshaller();

    Person person = (Person) unmarshaller.unmarshal(new File("person.xml"));
    System.out.println(person.getFirstName());
}

From source file:Main.java

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

    // Create the Object
    Main foo = new Main();
    foo.setBar("Hello World");

    // Create the Document
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document document = db.newDocument();

    // Marshal the Object to a Document
    Marshaller marshaller = jc.createMarshaller();
    marshaller.marshal(foo, document);// ww  w  . ja va2 s. c om

    // Output the Document
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer t = tf.newTransformer();
    DOMSource source = new DOMSource(document);
    StreamResult result = new StreamResult(System.out);
    t.transform(source, result);
}

From source file:Weird.java

public static void main(String[] args) throws JAXBException {
    Weird w = new Weird();
    w.value = "foo";
    w.svalue = "bar";
    JAXBContext context = JAXBContext.newInstance(Weird.class);
    context.createMarshaller().marshal(w, System.out);
}

From source file:Main.java

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

    Unmarshaller unmarshaller = jc.createUnmarshaller();
    File xml = new File("input.xml");
    Graph graph = (Graph) unmarshaller.unmarshal(xml);

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

From source file:Main.java

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

    Messages messages = new Messages();
    messages.getMessages().add(new Message());
    messages.getMessages().add(new Message());
    messages.getMessages().add(new Message());

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setAdapter(new IDAdapter());
    marshaller.marshal(messages, System.out);
}

From source file:Weird.java

public static void main(String[] args) throws JAXBException {
    Weird w = new Weird();
    w.myValue = "foo";
    w.svalue = "bar";
    JAXBContext context = JAXBContext.newInstance(Weird.class);
    context.createMarshaller().marshal(w, System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Root root = new Root();

    Book cDev = new Book();
    cDev.setName("C Development");
    root.getEmployeeDesiredSkills().add(cDev);

    Book perlDev = new Book();
    perlDev.setName("Perl Development");
    root.getEmployeeDesiredSkills().add(perlDev);

    JAXBContext jc = JAXBContext.newInstance(Root.class);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(root, System.out);
}

From source file:MisspelledPersonUnmarshaller.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance("person");
    Unmarshaller unmarshaller = context.createUnmarshaller();

    JAXBElement<Person> element = unmarshaller.unmarshal(new StreamSource("p.xml"), Person.class);
    Person person = element.getValue();//w  ww. j a va  2  s  .co m
    System.out.println(person.getFirstName());

}

From source file:Person1Test.java

public static void main(String[] args) throws JAXBException {
    Person person = new Person();
    person.setFirstName("L");
    person.setLastName("H");

    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(person, System.out);
}