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> void marshall(String file, JAXBElement<T> object, Class context) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(context);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.marshal(object, new File(file));
}

From source file:Main.java

public static <T> T unmarshall(String file, Class<T> desiredClass, Class context) throws Exception {
    JAXBContext ctx = JAXBContext.newInstance(context);
    Unmarshaller unMarshaller = ctx.createUnmarshaller();
    JAXBElement<T> object = (JAXBElement<T>) unMarshaller.unmarshal(new File(file));
    return object.getValue();
}

From source file:Main.java

public static Object parseXmlFileToBeanByJAXB(String path, Class clase) throws Exception {

    JAXBContext context = JAXBContext.newInstance(clase);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement elm = unmarshaller.unmarshal(new StreamSource(new File(path)), clase);

    return elm.getValue();

}

From source file:Main.java

public static String convertToXmlString(Object source) {
    try {//from w  ww .  j av a 2s.c o m
        StringWriter sw = new StringWriter();
        JAXBContext jAXBContext = JAXBContext.newInstance(source.getClass());
        Marshaller marshaller = jAXBContext.createMarshaller();
        marshaller.marshal(source, sw);
        return sw.toString();
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:Main.java

public static String asXml(Object object) throws IOException {
    try {//from   w ww . ja  v  a  2s .c om
        StringWriter writer = new StringWriter();
        JAXBContext context = JAXBContext.newInstance(object.getClass());
        Marshaller m = context.createMarshaller();
        m.marshal(object, writer);

        return writer.toString();

    } catch (Exception e) {
        return "";
    }
}

From source file:Main.java

public static <T> void marshal(JAXBElement<T> element, Class<T> c, OutputStream os) {
    try {/*from   w ww  .  ja v  a 2 s . co  m*/
        JAXBContext jc = JAXBContext.newInstance(c);
        Marshaller ma = jc.createMarshaller();
        ma.marshal(element, os);
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:Main.java

public static String objectToXML(Class clazz, Object object) throws JAXBException {
    String xml = null;/* www .j  a  v  a 2s  .  c o  m*/
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    Writer w = new StringWriter();
    m.marshal(object, w);
    xml = w.toString();
    return xml;
}

From source file:Main.java

public static <T> T unMarshal(String content, Class<T> clazz) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

    return unmarshaller.unmarshal(new StreamSource(new StringReader(content)), clazz).getValue();
}

From source file:Main.java

public static String parseBeanToXmlStringByJAXB(Object bean, Class clase) throws Exception {
    JAXBContext jc = JAXBContext.newInstance(clase);
    Marshaller marshaller = jc.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);
    JAXBElement<Object> rootElement = new JAXBElement<Object>(new QName(clase.getSimpleName()), clase, bean);

    OutputStream output = new OutputStream() {

        private StringBuilder string = new StringBuilder();

        public void write(int b) throws IOException {
            this.string.append((char) b);
        }/*from w w w . j a v  a  2  s .  c  om*/

        //Netbeans IDE automatically overrides this toString()
        public String toString() {
            return this.string.toString();
        }
    };

    marshaller.marshal(rootElement, output);

    return output.toString();

}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xml2obj(String xml, Class<T> type) {
    T obj = null;//ww  w.  j a  v a 2s . co m
    try {
        JAXBContext context = JAXBContext.newInstance(type);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        obj = (T) unmarshaller.unmarshal(new StringReader(xml));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return obj;
}