Java tutorial
//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); } 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; } }