Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

In this page you can find the example usage for javax.xml.bind Marshaller marshal.

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:Main.java

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

    Customer customer = new Customer();
    customer.setFirstName("Jane");
    customer.setLastName("Doe");

    PhoneNumber workPhone = new PhoneNumber();
    workPhone.setType("work");
    workPhone.setNumber("555-1111");
    customer.getPhoneNumbers().add(workPhone);

    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<Customer> rootElement = new JAXBElement<Customer>(new QName("customer"), Customer.class,
            customer);/* www.  ja va 2s  .c  om*/
    marshaller.marshal(rootElement, System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLStreamWriter writer = XMLOutputFactory.newFactory().createXMLStreamWriter(System.out);
    writer.setDefaultNamespace("http://www.java2s.com");

    JAXBContext jc = JAXBContext.newInstance(WorkSet.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    writer.writeStartDocument();//from  ww  w  .ja  va 2s.  c o m
    writer.writeStartElement("http://www.java2s.com", "Import");
    writer.writeNamespace("", "http://www.java2s.com");
    writer.writeStartElement("WorkSets");

    m.marshal(new WorkSet(), writer);
    m.marshal(new WorkSet(), writer);

    writer.writeEndDocument();
    writer.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    JAXBContext context = JAXBContext.newInstance(Disk.class, MyStatus.class, MyDisk.class);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    Disk disk = new Disk();
    disk.setStatus("attached");
    disk.setSize(10000000000L);//  ww w  .ja va  2s .  c  o  m
    disk.setFreeSpace(25600000L);
    disk.setId("1");

    m.marshal(disk, System.out);
    m.marshal(new MyStatus(disk), System.out);
    m.marshal(new MyDisk(disk), System.out);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Element> elementList = new ArrayList<Element>();
    List<Item> itemList = new ArrayList<Item>();
    Element element1 = new Element();
    Element element2 = new Element();
    Item item1 = new Item();
    Item item2 = new Item();
    Elements elements = new Elements();

    item1.setId(1);//from   ww  w.  j  a  v  a2 s .co  m
    item1.setName("Test1");
    item2.setId(2);
    item2.setName("Test2");
    itemList.add(item1);
    itemList.add(item2);

    element1.setProperty1("prop1");
    element1.setProperty2("prop2");
    element1.setType(2);
    element1.setItems(itemList);

    element2.setProperty1("prop11");
    element2.setProperty2("prop22");
    element2.setType(22);
    element2.setItems(itemList);

    elementList.add(element1);
    elementList.add(element2);

    elements.setElements(elementList);

    System.out.println("------- Object to XML -----------\n");
    JAXBContext jaxbContext = JAXBContext.newInstance(Elements.class);
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    // output pretty printed
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    jaxbMarshaller.marshal(elements, System.out);

    System.out.println("\n------- XML to Object -----------\n");

    String xml = "<elements><element><items><item><id>1</id><name>Test1</name></item><item><id>2</id><name>Test2</name></item></items><property1>prop1</property1><property2>prop2</property2><type>2</type></element><element><items><item><id>1</id><name>Test1</name></item><item><id>2</id><name>Test2</name></item></items><property1>prop11</property1><property2>prop22</property2><type>22</type></element></elements>";
    StringReader reader = new StringReader(xml);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Elements elementsOut = (Elements) jaxbUnmarshaller.unmarshal(reader);
    System.out.println(elementsOut);

}

From source file:Main.java

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

    PayTypeList paymentType = new PayTypeList();

    List<String> paymentTypes = new ArrayList<String>();
    paymentTypes.add("one");
    paymentTypes.add("two");
    paymentTypes.add("three");
    paymentType.setPayType(paymentTypes);

    m.marshal(paymentType, System.out);
}

From source file:Main.java

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

    Root rootA = new Root();
    rootA.setName("A");

    Root rootB = new Root();
    rootB.setName("B");
    rootA.setChild(rootB);//from   www .j  a  v  a 2 s  . c o m

    Root rootC = new Root();
    rootC.setName("C");
    rootB.setChild(rootC);

    Root rootD = new Root();
    rootD.setName("D");
    rootC.setChild(rootD);

    Root rootE = new Root();
    rootE.setName("E");
    rootD.setChild(rootE);

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

    DepthListener depthListener = new DepthListener(3);
    marshaller.setListener(depthListener);
    marshaller.setAdapter(new RootAdapter(depthListener));
    marshaller.marshal(rootA, 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);//w ww . j  a va2s . c o  m

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

From source file:MarshalValidation.java

public static void main(String[] args) throws Exception {
    Person p = new Person();
    p.setFirstName("B");
    p.setLastName("H");

    JAXBContext context = JAXBContext.newInstance(Person.class);
    Marshaller marshaller = context.createMarshaller();

    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(new File("person.xsd"));

    marshaller.setSchema(schema);/*from   w  w w.  ja v  a 2  s  .  com*/
    marshaller.setEventHandler(new ValidationEventHandler() {
        public boolean handleEvent(ValidationEvent event) {
            System.out.println(event);
            return false;
        }
    });

    marshaller.marshal(p, System.out);
}

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

public static void main(String[] args) throws Exception {
    String dir = args[0];//  w  w w  .  j  a  v a 2s  .  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:org.jasig.portlet.announcements.Exporter.java

public static void main(String[] args) throws Exception {

    String dir = args[0];//from   w  w  w .  j  a v a  2  s  .c o  m
    ApplicationContext context = PortletApplicationContextLocator
            .getApplicationContext(PortletApplicationContextLocator.DATABASE_CONTEXT_LOCATION);
    SessionFactory sessionFactory = context.getBean(SESSION_FACTORY_BEAN_NAME, SessionFactory.class);
    IAnnouncementService announcementService = context.getBean(ANNOUNCEMENT_SVC_BEAN_NAME,
            IAnnouncementService.class);

    Session session = sessionFactory.getCurrentSession();
    Transaction transaction = session.beginTransaction();

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

    List<Topic> topics = announcementService.getAllTopics();
    for (Topic topic : topics) {
        if (topic.getSubscriptionMethod() == 4) {
            continue;
        }

        session.lock(topic, LockMode.NONE);
        JAXBElement<Topic> je2 = new JAXBElement<Topic>(new QName("topic"), Topic.class, topic);
        String output = dir + File.separator + UUID.randomUUID().toString() + ".xml";
        System.out.println("Exporting Topic " + topic.getId() + " to file " + output);
        try {
            marshaller.marshal(je2, new FileOutputStream(output));
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }
    transaction.commit();
}