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 main(String[] args) throws JAXBException {
    HumansList list = new HumansList();
    Person parent1 = new Person("parent1");
    list.addHuman(parent1);//w ww  . j  a v  a 2 s .  co m
    Person child11 = new Person("child11");
    list.addHuman(child11);
    Person child12 = new Person("child12");
    list.addHuman(child12);

    Person parent2 = new Person("parent2");
    list.addHuman(parent2);
    Person child21 = new Person("child21");
    list.addHuman(child21);
    Person child22 = new Person("child22");
    list.addHuman(child22);

    JAXBContext context = JAXBContext.newInstance(HumansList.class);
    Marshaller m = context.createMarshaller();
    StringWriter xml = new StringWriter();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

    m.marshal(list, xml);
    System.out.println(xml);
}

From source file:Main.java

public static File Marshall(Object object, String filePath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    Marshaller u = jc.createMarshaller();
    u.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    File f = new File(filePath);
    u.marshal(object, f);//from ww w  .  j a v  a  2  s  .  c o m
    return f;
}

From source file:Main.java

public static <T> void JAXBMarshalling(T object, File output) throws JAXBException {
    JAXBContext jAXBContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jAXBContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    marshaller.marshal(object, output);/*from w w w .j a  va2 s . c o  m*/
}

From source file:Main.java

public static String marshallToStringNoValidation(final Object xmlObject) throws JAXBException {
    StringWriter writer = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(xmlObject.getClass());
    Marshaller jaxbMarshaller = context.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(xmlObject, writer);
    return writer.toString();
}

From source file:Main.java

private static <T> Marshaller createMarshaller(Class<T> clazz, Marshaller.Listener listener)
        throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Marshaller marshaller = jc.createMarshaller();
    if (listener != null) {
        marshaller.setListener(listener);
    }//from w w  w. j  a  v a2  s . co m
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    return marshaller;
}

From source file:Main.java

public static String toXML(Object obj) {
    try {/*w  w  w .jav  a 2s .c o m*/
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        return writer.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
        return "";
    }
}

From source file:Main.java

public static <T> void writeXml(Class<T> _class, T jaxbElement, OutputStream outputStream, String encoding)
        throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(_class);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

    marshaller.marshal(jaxbElement, outputStream);
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;//from  w ww.j  a  va  2  s  . c  om
    try {
        JAXBContext context = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);

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

    return result;
}

From source file:Main.java

public static String obj2Xml(Object obj, String encoding) {
    String result = null;//  w  w w.  java2s  . 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);
        StringWriter writer = new StringWriter();
        marshaller.marshal(obj, writer);
        result = writer.toString();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return result;
}

From source file:Main.java

public static void saveInstance(OutputStream outputStream, 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, outputStream);

    outputStream.flush();/*from w w w . ja  v  a  2  s.c o  m*/

}