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 toXml(T object) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);/*from  ww w.  j a  v a  2s  .  com*/
    return writer.toString();
}

From source file:Main.java

public static void parseBeanToXmlFileByJAXB(String path, Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);
    marshaller.marshal(rootElement, new FileOutputStream(path));
}

From source file:Main.java

public final static void save(Object object, String path) {
    try {//  ww w .j a va2s  .c  o m
        File file = new File(path);
        JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(object, file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void saveInstance(File outputFile, Object instance) throws JAXBException, IOException {
    Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(instance, outputFile);
}

From source file:Main.java

public static Object xmlToPojo(Object o, String arquivo) throws JAXBException {
    File file = new File(arquivo);

    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

    return jaxbUnmarshaller.unmarshal(file);

}

From source file:Main.java

public final static Object load(Class clazz, String name) {
    try {/*  ww w .j a  v a2 s . c o m*/
        File file = new File(name);
        JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        return jaxbUnmarshaller.unmarshal(file);
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T fromXml(String xmlStr, Class<T> klass) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(klass);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    return t;/*from  ww w  .jav  a  2 s. co m*/
}

From source file:Main.java

public static String toXML(Object obj) {
    try {/*from   w  w  w  . j a v a  2  s.  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 Object convertToPojoUsingFile(String fileName, Class... type) {
    Object result;//from  w  w  w. j  a  v  a2 s.  c  o m
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        result = unmarshaller.unmarshal(new File(fileName));
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unserializer(Class<T> clazz, String xml) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T obj = (T) unmarshaller.unmarshal(new StringReader(xml));
    return obj;//w  w  w .  j  a va2 s . c  om
}