Example usage for javax.xml.bind JAXBContext newInstance

List of usage examples for javax.xml.bind JAXBContext newInstance

Introduction

In this page you can find the example usage for javax.xml.bind JAXBContext newInstance.

Prototype

public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException 

Source Link

Document

Create a new instance of a JAXBContext class.

Usage

From source file:Main.java

public static <T> String toString(T xml) {
    JAXBContext jc;/*from  www  .jav a  2 s  . c o m*/
    try {
        jc = JAXBContext.newInstance(xml.getClass());
        Marshaller m = jc.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter writer = new StringWriter();
        m.marshal(xml, writer);
        return writer.toString();
    } catch (JAXBException e) {
        return "";
    }
}

From source file:Main.java

public static <T> String marshal(T object) throws JAXBException {
    final Marshaller marshaller = JAXBContext.newInstance(object.getClass()).createMarshaller();
    final StringWriter stringWriter = new StringWriter();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(object, stringWriter);

    return stringWriter.toString();
}

From source file:Main.java

public static <T> T unmarshall(InputStream fileStream, Class<?>... clazz) throws Exception {

    JAXBContext context = null;/*ww w  .java 2 s.c  om*/
    context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    return (T) unmarshaller.unmarshal(fileStream);

}

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);/* w w w. ja v a2s  .c o m*/
}

From source file:Main.java

public static Object xmlToObject(Class clazz, String xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller um = context.createUnmarshaller();
    return um.unmarshal(new StringReader(xml));
}

From source file:Main.java

public static <T> void xmlWriter(T entity, String xmlDestinationPath) {
    try {/* w ww. j  a  v a  2s.  co m*/
        JAXBContext jc = JAXBContext.newInstance(entity.getClass());
        File destinationFile = new File(xmlDestinationPath);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(entity, destinationFile);
    } catch (JAXBException ex) {
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T fromXML(String xml, Class<T> valueType) {
    try {/*from w  ww  .j a v  a 2  s .  c om*/
        JAXBContext context = JAXBContext.newInstance(valueType);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        throw new RuntimeException(e.getMessage());
    }
}

From source file:Main.java

public static <T> String generateObject2XMLString(T object) throws JAXBException {

    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter sw = new StringWriter();
    jaxbMarshaller.marshal(object, sw);/*  ww  w. j  ava 2  s  .  co  m*/

    return sw.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  .  com*/
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    return marshaller;
}

From source file:Main.java

public static Marshaller getMarshaller(Class jaxbClass) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    return marshaller;
}