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> T reload(InputStream is, Class<?>... clazz) throws JAXBException {
    //Create JAXB Context
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller um = jc.createUnmarshaller();
    return (T) um.unmarshal(is);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T unmarshal(Class<T> clazz, InputStream is) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Unmarshaller um = jc.createUnmarshaller();
    return (T) um.unmarshal(is);
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Object convertXmlStringToObject(String xml, Class clazz) {
    Object object = null;//from  ww  w.  j a  va 2s. c  o  m
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        StringReader xmlReader = new StringReader(xml);
        object = unmarshaller.unmarshal(xmlReader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return object;
}

From source file:Main.java

public static void writeXML(Class<?> class1, Object obj, File outputFile)
        throws JAXBException, FileNotFoundException {
    JAXBContext context = JAXBContext.newInstance(class1);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(obj, outputFile);// w w w  .  j  a va2  s. c  om
}

From source file:Main.java

public static <T> String print(T xml, Class<?>... clazz) throws JAXBException {
    //Create JAXB Context
    JAXBContext jc = JAXBContext.newInstance(clazz);

    //Create marshaller
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();
    marshaller.marshal(xml, writer);/*from www  .  ja  v a2  s  .  c  o  m*/

    return writer.toString();
}

From source file:Main.java

public static String toXxml(Object bean) {
    StringWriter stringWriter = null;
    try {/*www . ja v  a 2s  . c o m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass());
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        stringWriter = new StringWriter();
        marshaller.marshal(bean, stringWriter);
        String result = stringWriter.toString();
        // remove xml declaration
        result = result.replaceFirst(".*\n", "");
        return result;
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    } finally {
        if (stringWriter != null)
            try {
                stringWriter.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }
}

From source file:Main.java

public static <T> T UnMarshall(Class<T> objectClass, String filePath) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(objectClass);
    Unmarshaller u = jc.createUnmarshaller();
    File f = new File(filePath);
    return objectClass.cast(u.unmarshal(f));
}

From source file:Main.java

public static String convertToXml(Object source, Class... type) {
    String result;/* www. ja  v a 2  s  .c  om*/
    StringWriter sw = new StringWriter();
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Marshaller marshaller = context.createMarshaller();
        marshaller.marshal(source, sw);
        result = sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }

    return result;
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static Object convertXmlPathToObject(String xmlPath, Class clazz) {
    Object object = null;// www.  j  a v a  2  s  . co  m
    try {
        JAXBContext context = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        FileReader xmlReader = null;
        try {
            xmlReader = new FileReader(xmlPath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        object = unmarshaller.unmarshal(xmlReader);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return object;
}

From source file:Main.java

public static <T> T asObject(String xml, Class<T> clazz) {
    try {/*w  w  w. j a v  a  2s  . c  o m*/
        JAXBContext jaxbContext = null;
        jaxbContext = JAXBContext.newInstance(clazz);
        Unmarshaller unmarshaller = null;
        unmarshaller = jaxbContext.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return null;
}