Example usage for javax.xml.bind JAXBContext createMarshaller

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

Introduction

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

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

From source file:MisspelledPersonUnmarshaller.java

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

From source file:Main.java

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);
}

From source file:Main.java

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

    A a = new A();
    a.setFoo("Hello World");
    marshaller.marshal(a, System.out);

    B b = new B();
    b.setFoo("Hello World");
    marshaller.marshal(b, System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Main customer = new Main();
    customer.setId(100);/*from ww  w. j  av a2 s  .  c o  m*/
    customer.setName("java2s.com");
    customer.setAge(29);

    File file = new File("file.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(Main.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(customer, file);
    jaxbMarshaller.marshal(customer, System.out);

}

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(Vehicals.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Vehicals vehicals = new Vehicals();

    List<Car> cars = new ArrayList<Car>();
    Car c = new Car();
    c.setName("Mercedes");
    cars.add(c);/* ww w  . jav  a2s.c o m*/

    c = new Car();
    c.setName("BMW");
    cars.add(c);
    vehicals.setCar(cars);
    m.marshal(vehicals, System.out);
}

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);
}

From source file:Main.java

public static void main(String[] args) throws JAXBException {
    HumansList list = new HumansList();
    Person parent1 = new Person("parent1");
    list.addHuman(parent1);//from w w  w  . j  a v  a 2 s. c o m
    Person child11 = new Person("child11");
    list.addHuman(child11);
    Person child12 = new Person("child12");
    list.addHuman(child12);

    Person parent2 = new Person("parent2");
    list.addHuman(parent2);
    Person child21 = new Person("child21");
    list.addHuman(child21);
    Person child22 = new Person("child22");
    list.addHuman(child22);

    JAXBContext context = JAXBContext.newInstance(HumansList.class);
    Marshaller m = context.createMarshaller();
    StringWriter xml = new StringWriter();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

    m.marshal(list, xml);
    System.out.println(xml);
}

From source file:org.jasig.portlet.data.Exporter.java

public static void main(String[] args) throws Exception {
    String dir = args[0];//w w w .ja  va 2 s .c  o  m
    String importExportContext = args[1];
    String sessionFactoryBeanName = args[2];
    String modelClassName = args[3];
    String serviceBeanName = args[4];
    String serviceBeanMethodName = args[5];

    ApplicationContext context = PortletApplicationContextLocator.getApplicationContext(importExportContext);
    SessionFactory sessionFactory = context.getBean(sessionFactoryBeanName, SessionFactory.class);
    Class<?> modelClass = Class.forName(modelClassName);

    Object service = context.getBean(serviceBeanName);
    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.beginTransaction();

    JAXBContext jc = JAXBContext.newInstance(modelClass);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Method method = service.getClass().getMethod(serviceBeanMethodName);
    List<?> objects = (List<?>) method.invoke(service, null);

    for (Object o : objects) {
        session.lock(o, LockMode.NONE);
        JAXBElement je2 = new JAXBElement(new QName(modelClass.getSimpleName().toLowerCase()), modelClass, o);
        String output = dir + File.separator + UUID.randomUUID().toString() + ".xml";
        try {
            marshaller.marshal(je2, new FileOutputStream(output));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    transaction.commit();
}

From source file:Main.java

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

    PersonTraining pt = new PersonTraining();

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(pt, 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);
}