Example usage for javax.xml.bind Marshaller JAXB_ENCODING

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

Introduction

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

Prototype

String JAXB_ENCODING

To view the source code for javax.xml.bind Marshaller JAXB_ENCODING.

Click Source Link

Document

The name of the property used to specify the output encoding in the marshalled XML data.

Usage

From source file:Main.java

public static void marshal(Class<?> klass, Object obj, OutputStream os) throws JAXBException {
    try {//ww  w  .ja  va2 s  .com
        context = JAXBContext.newInstance(klass);
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        unmarshaller = context.createUnmarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(
                "There was a problem creating a JAXBContext object for formatting the object to XML.");
    }

    try {
        marshaller.marshal(obj, os);
    } catch (JAXBException jaxbe) {
        jaxbe.printStackTrace();
    }
}

From source file:Main.java

public static String convertBean2Xml(Object obj) throws IOException {
    String result = null;/*from  w w w  . j  a  va  2 s .c  om*/
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance();
        XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
                (String) marshaller.getProperty(Marshaller.JAXB_ENCODING));
        xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
        marshaller.marshal(obj, xmlStreamWriter);
        xmlStreamWriter.writeEndDocument();
        xmlStreamWriter.close();
        result = baos.toString("UTF-8");

    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    } catch (XMLStreamException e) {
        e.printStackTrace();
        return null;
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
        return null;
    }
    return result;
}

From source file:Main.java

public static String bean2Xml(Object bean) {
    String xmlString = null;/*from  www.j  av a  2  s.  c o  m*/
    JAXBContext context;
    StringWriter writer;
    if (null == bean)
        return xmlString;
    try {
        context = JAXBContext.newInstance(bean.getClass());
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);//
        //m.setProperty(Marshaller.JAXB_ENCODING, "gb2312");//
        //m.setProperty(Marshaller.JAXB_ENCODING, "GBK");//
        m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");//
        m.setProperty(Marshaller.JAXB_FRAGMENT, false);//
        writer = new StringWriter();
        m.marshal(bean, writer);
        xmlString = writer.toString();
        return xmlString;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return xmlString;
}

From source file:Main.java

public static String marshalToString(Class<?> klass, Object obj) throws JAXBException {
    try {/*from  ww w .  j  ava2 s  .  com*/
        context = JAXBContext.newInstance(klass);
        marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        unmarshaller = context.createUnmarshaller();
    } catch (JAXBException e) {
        throw new RuntimeException(
                "There was a problem creating a JAXBContext object for formatting the object to XML.");
    }
    StringWriter sw = new StringWriter();
    String result = null;
    try {
        marshaller.marshal(obj, sw);
        result = sw == null ? null : sw.toString();
    } catch (JAXBException jaxbe) {
        jaxbe.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static String marshal(Object object) throws Exception {
    StringWriter string = new StringWriter();
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$
    marshaller.marshal(object, string);// w ww .  j av  a  2  s .co  m
    return string.toString();
}

From source file:Main.java

public static OutputStream serializerOutputStream(Object xmlObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(xmlObj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(xmlObj, baos);/*from  w w  w .j a  v a 2  s. co  m*/
    return baos;
}

From source file:Main.java

/**
 * Marshal a JAXB element to a XML DOM document
 *
 * @param jaxbElement//w w  w.  j a  v a  2  s . com
 *            The root of content tree to be marshalled
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document doMarshallingJAXBObject(Object jaxbElement) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    Document doc = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.newDocument();
        marshaller.marshal(jaxbElement, doc);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    }

    return doc;
}

From source file:Main.java

public static void marshal(Object model, OutputStream output) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    jaxbMarshaller.marshal(model, output);
}

From source file:Main.java

/**
 * Marshal a JAXB element to a XML file//  w ww  . jav  a  2 s . co  m
 *
 * @param jaxbElement
 *            The root of content tree to be marshalled
 * @param strXMLFilePath
 *            XML output file path
 * @throws Exception
 *             in error case
 */
public static void doMarshalling(Object jaxbElement, String strXMLFilePath) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    if (strXMLFilePath == null) {
        throw new RuntimeException("No XML file path (null)!");
    }
    FileOutputStream fos = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        fos = new FileOutputStream(strXMLFilePath);
        marshaller.marshal(jaxbElement, fos);// System.out);
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    } finally {
        if (fos != null) {
            fos.close();
            fos = null;
        }
    }
}

From source file:Main.java

public static Marshaller createMarshaller(String pack, Schema schema) {
    JAXBContext jaxbContext = null;
    try {/*from w  ww .  ja  va 2 s . com*/
        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;
}