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 saveInstance(Writer output, Object instance)

        throws JAXBException, IOException {

    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING);

    marshaller.marshal(instance, output);

    output.flush();/* w  ww  .  j a va 2 s .co m*/

}

From source file:Main.java

public static String bean2Xml(Object bean, String codetype) {
    String xmlString = null;/*from w  w w.  ja  v  a2  s .co  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, codetype);//
        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 void serialize(Object o, OutputStream os, Boolean format) throws JAXBException {
    String pkg = o.getClass().getPackage().getName();
    JAXBContext jc = getCachedContext(pkg);
    Marshaller m = jc.createMarshaller();
    m.setEventHandler(new DefaultValidationEventHandler());
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    m.marshal(o, os);//from ww w  . jav a  2  s. co m
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;/*from   www .ja  v a  2 s . com*/
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        //         marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
        //         marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, true);

        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return result;
}

From source file:Main.java

public final String getMessage() throws Exception {

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty("jaxb.encoding", "ISO-8859-1");
    jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos,
            (String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING));
    xmlStreamWriter.writeStartDocument((String) jaxbMarshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0");
    jaxbMarshaller.marshal(this, xmlStreamWriter);
    xmlStreamWriter.writeEndDocument();//  w ww.ja  va 2 s .  c  om
    xmlStreamWriter.close();
    return new String(baos.toByteArray());
}

From source file:com.iflytek.edu.cloud.frame.spring.Jaxb2RootElementHttpMessageConverterExt.java

@Override
protected void customizeMarshaller(Marshaller marshaller) {
    try {/*w w  w . java 2  s  .  c  om*/
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    } catch (PropertyException e) {
        LOGGER.error(e.getMessage(), e);
    }
}

From source file:Main.java

/**
 * Create a marshaller for XML generation.
 *
 * @param encoding//from   w  ww .j a  v a 2  s.  com
 * encoding of generated XML output
 *
 * @param prettyPrint
 * enable pretty printing for generated XML output
 *
 * @return
 * marshaller
 *
 * @throws JAXBException
 * if the marshaller is not creatable
 */
public static Marshaller createMarshaller(String encoding, boolean prettyPrint) throws JAXBException {
    Marshaller m = getContext().createMarshaller();
    m.setProperty(Marshaller.JAXB_ENCODING, encoding);
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, prettyPrint);
    return m;
}

From source file:Main.java

/**
 * Creates a {@link Marshaller} based on the given {@link JAXBContext}
 * and configures it to use the given {@link Charset}, and allows to
 * output the XML code to be generated formatted and as an XML fragment.
 * /*www  . ja  va  2  s  .  c  o  m*/
 * @param ctx the {@link JAXBContext} to create a {@link Marshaller} for
 * @param charset the {@link Charset} the XML code should be formatted
 * @param formatted {@code true} if the XML code should be formatted,
 *       {@code false} otherwise
 * @param fragment {@code false} if the XML code should start with
 *       {@code <?xml }, or {@code true} if just fragment XML code should
 *       get generated
 * 
 * @return a preconfigured {@link Marshaller}
 * 
 * @throws IOException in case no {@link Marshaller} could get created
 */
public static Marshaller createMarshaller(JAXBContext ctx, Charset charset, boolean formatted, boolean fragment)
        throws IOException {
    if (charset == null) {
        return createMarshaller(ctx, Charset.defaultCharset(), formatted, fragment);
    }
    Objects.requireNonNull(ctx);
    try {
        Marshaller marshaller = ctx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatted);
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, fragment);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, charset.name());
        return marshaller;
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}

From source file:org.esbtools.gateway.GatewayRequest.java

public String toXML() {
    StringWriter thisXML = new StringWriter();

    try {//from   w  w w. j  a  v a  2  s. co m
        JAXBContext jaxbContext = JAXBContext.newInstance(this.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.marshal(this, thisXML);

    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return thisXML.toString();
}

From source file:com.evolveum.midpoint.cli.common.ToolsUtils.java

public static void serializeObject(Object object, Writer writer) throws JAXBException {
    if (object == null) {
        return;/* w ww  .  j  av  a  2  s .c  o  m*/
    }

    Marshaller marshaller = JAXB_CONTEXT.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, StandardCharsets.UTF_8.name());
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);

    if (object instanceof ObjectType) {
        object = new JAXBElement(C_OBJECT, Object.class, object);
    }

    marshaller.marshal(object, writer);
}