Java XML JAXB Marshaller createMarshaller(String pack)

Here you can find the source of createMarshaller(String pack)

Description

create Marshaller

License

Apache License

Declaration

public static Marshaller createMarshaller(String pack) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import javax.xml.validation.Schema;

public class Main {
    public static Marshaller createMarshaller(String pack) {
        return createMarshaller(pack, null);
    }/*from w  ww  .jav  a2 s . com*/

    public static Marshaller createMarshaller(String pack, Schema schema) {
        JAXBContext jaxbContext = null;
        try {
            jaxbContext = JAXBContext.newInstance(pack);
            Marshaller marsh = jaxbContext.createMarshaller();

            if (schema != null) {
                marsh.setSchema(schema);
                //                marsh.setEventHandler( new DefaultValidationEventHandler() {
                //                    @Override
                //                    public boolean handleEvent( ValidationEvent event ) {
                //                        return super.handleEvent( event );
                //                    }
                //                });
            }
            marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
            marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            return marsh;
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. createMarshaller(Class clazz, Map settings)
  2. createMarshaller(Class clazz)
  3. createMarshaller(JAXBContext context)
  4. createMarshaller(JAXBContext ctx, boolean indent)
  5. createMarshaller(Object object)
  6. getJSIDLMarshaller()
  7. getMarshaller()
  8. getMarshaller()
  9. getMarshaller(boolean prettyFormat)