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 Unmarshaller getUnmarshaller(Class jaxbClass) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(jaxbClass);
    return jaxbContext.createUnmarshaller();
}

From source file:Main.java

public static <T> Object JAXBUnmarshalling(Class insClass, String fileSource) throws JAXBException {
    JAXBContext jAXBContext = JAXBContext.newInstance(insClass);
    Unmarshaller unmarshaller = jAXBContext.createUnmarshaller();

    File file = new File(fileSource);
    Object object = unmarshaller.unmarshal(file);
    return object;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T parseXml(File file, Class<T> clazz) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Unmarshaller shaller = context.createUnmarshaller();
    return (T) shaller.unmarshal(file);
}

From source file:Main.java

public static <T> Object fromXml(String xmlStr, Class<T> pojoClass) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(pojoClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    Object object = unmarshaller.unmarshal(new ByteArrayInputStream(xmlStr.getBytes()));
    return object;
}

From source file:Main.java

public static Object loadInstance(InputStream inputStream, Class instanceClass) throws JAXBException {
    Unmarshaller unmarshaller = JAXBContext.newInstance(instanceClass).createUnmarshaller();
    Object object = unmarshaller.unmarshal(inputStream);
    return object;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T ToJavaBean(String xml, Class<T> c) {
    T t = null;//from ww w .  j a  v  a 2s  .  c o  m
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return t;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;// w w w  .ja  v  a 2 s. co m
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        System.out.println(e);
    }
    return t;
}

From source file:Main.java

public static void pojoToXml(Object o, String arquivo) throws JAXBException {
    File file = new File(arquivo);
    JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(o, file);/*from www.  j a  va2 s.c o  m*/
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) {
    T t = null;//ww w .  j a  va 2 s .  co m
    try {
        JAXBContext context = JAXBContext.newInstance(c);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        t = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (Exception e) {
        e.printStackTrace();
    }

    return t;
}

From source file:Main.java

public static <T> T deserializeJaxb(Class<T> cls, Reader reader) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(cls);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    return (T) unmarshaller.unmarshal(reader);
}