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

@SuppressWarnings("unchecked")
public static <T> T xmlToObject(String str, Class<T> clszz) {
    JAXBContext context = null;/*from   www  .j  a v a 2 s .  com*/
    T obj = null;

    try {
        context = JAXBContext.newInstance(clszz);
        StringReader reader = new StringReader(str);
        Unmarshaller unmar = context.createUnmarshaller();
        obj = (T) unmar.unmarshal(reader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}

From source file:Main.java

@SuppressWarnings("rawtypes")
public static Object unmarshal(String xmlBody, Class objectClass) {
    try {/*from   w w  w . ja v  a2 s. com*/
        JAXBContext context = JAXBContext.newInstance(objectClass);
        Unmarshaller u = context.createUnmarshaller();
        return u.unmarshal(new StreamSource(new StringReader(xmlBody)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static <T> T converyToJavaBean(String xmlStr, Class<T> c) {
    T t = null;//from  w  w w  .j  av  a  2  s  .  co m
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xmlStr));
    } catch (Exception e) {
        e.printStackTrace();
    }
    return t;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T convertToObject(String xml, Class<T> type) {
    StringReader sr = new StringReader(xml);
    try {//from  w w w  .  j a  va2  s . c  o m
        JAXBContext jAXBContext = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(sr);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

}

From source file:Main.java

public static JAXBElement<?> xmlToJaxb(Class<?> xmlClass, String xml) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<?> element;
    try (StringReader reader = new StringReader(xml)) {
        element = (JAXBElement<?>) unmarshaller.unmarshal(reader);
    }//from  w  w  w  .  java  2 s  . c o  m
    return element;
}

From source file:Main.java

public static Object convertXmlFileToObject(Class clazz, String xmlPath) {
    Object xmlObject = null;//from  w w  w .  j  a  v  a  2  s . c  om
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        FileReader fr = null;
        try {
            fr = new FileReader(xmlPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        xmlObject = unmarshaller.unmarshal(fr);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return xmlObject;
}

From source file:Main.java

public static String beanToXml(Object to) {
    try {/*from   ww w.ja va  2  s  .com*/
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        JAXBContext context = JAXBContext.newInstance(to.getClass());
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(to, out);
        return new String(out.toByteArray(), "UTF-8");
    } catch (JAXBException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static String convertToXml(Object obj, String encoding) {
    String result = null;//from  w ww  .jav  a 2 s.co  m
    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 <T> String obj2Xml(T c) throws RuntimeException {
    String returnXml = "";
    Writer writer = null;// w  w w. j  a v  a 2s .  c  o m
    try {
        writer = new StringWriter();
        JAXBContext.newInstance(c.getClass()).createMarshaller().marshal(c, writer);
        returnXml = writer.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            writer.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    return returnXml;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xml2Bean(String xml, Class<?> clazz) throws Exception {
    StringReader reader = null;//from  ww  w  .j a  va 2  s.c  om
    try {
        JAXBContext jc = JAXBContext.newInstance(new Class[] { clazz });
        Unmarshaller m = jc.createUnmarshaller();
        reader = new StringReader(xml);
        return (T) m.unmarshal(reader);
    } catch (Exception e) {
    } finally {
        close(reader);
    }
    return null;
}