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

/**
 * Reads an XML file and returns the content as an object of the specified class. 
 * @param file               XML file./* w  w  w .j a v  a2s  .  co  m*/
 * @param typeParameterClass   Class of the main object in the XML file.
 * @return                  Content as an object of the specified class.
 */
@SuppressWarnings("unchecked")
public static <T> T read(File file, Class<T> typeParameterClass) {
    T content = null;
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass);

        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        //jaxbUnmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");

        content = (T) jaxbUnmarshaller.unmarshal(file);
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return content;
}

From source file:Main.java

public static OutputStream serializerOutputStream(Object xmlObj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(xmlObj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    marshaller.marshal(xmlObj, baos);/*  www . java2s. c om*/
    return baos;
}

From source file:Main.java

public static void marshal(Object model, OutputStream output) throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
    jaxbMarshaller.marshal(model, output);
}

From source file:Main.java

public static <T> Element marshal(JAXBElement<T> jaxbElement, Class<T> cls) {
    try {/*w  w w.  j a v  a  2  s.c  o  m*/
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();

        JAXBContext jaxbContext = JAXBContext.newInstance(cls);
        Marshaller marshaller = jaxbContext.createMarshaller();

        marshaller.marshal(jaxbElement, doc);

        return doc.getDocumentElement();
    } catch (JAXBException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ParserConfigurationException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Marshal a JAXB element to a XML DOM document
 *
 * @param jaxbElement//from ww w  .jav a  2s .c  om
 *            The root of content tree to be marshalled
 * @return XML DOM document
 * @throws Exception
 *             in error case
 */
public static Document doMarshallingJAXBObject(Object jaxbElement) throws Exception {
    if (jaxbElement == null) {
        throw new RuntimeException("No JAXB element to marshal (null)!");
    }
    Document doc = null;
    try {
        JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName());
        Marshaller marshaller = jaxbCtx.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.newDocument();
        marshaller.marshal(jaxbElement, doc);
        doc.getDocumentElement().normalize();
    } catch (Exception e) {
        //            Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error);
        throw e;
    }

    return doc;
}

From source file:Main.java

public static <T> T unmarshal(InputStream in, Class... boundClasses) {
    try {//  w  w  w  .j a v  a  2  s .co m
        Unmarshaller u = JAXBContext.newInstance(boundClasses).createUnmarshaller();
        return (T) u.unmarshal(in);
    } catch (Exception ex) {
    }
    return null;
}

From source file:Main.java

/**
 * Helper method to serialize an object to XML. Requires object to be Serializable
 * @param entity Object to serialize//from www .  jav  a2s . com
 * @param <T> class that implements Serializable
 * @return String XML representation of object
 * @throws IOException if errors during serialization
 * @throws JAXBException if errors during serialization
 */
public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException {
    StringWriter sw = new StringWriter();

    if (null != entity) {
        JAXBContext ctx = JAXBContext.newInstance(entity.getClass());

        Marshaller m = ctx.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        m.marshal(entity, sw);
    }
    sw.flush();
    sw.close();

    return sw.toString();
}

From source file:Main.java

public static void marshal(Object obj, OutputStream out, Class... boundClasses) {
    try {/*w ww  .j a va  2s. c om*/
        Marshaller m = JAXBContext.newInstance(boundClasses).createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        m.marshal(obj, out);
    } catch (Exception ex) {
    }
}

From source file:Main.java

@SuppressWarnings("unchecked")
public static <T> T xmlToBean(InputStream input, Class<T> t) {
    try {//w ww .j  a v  a  2  s .  c  o m
        JAXBContext context = JAXBContext.newInstance(t);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        return (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8"));
    } catch (JAXBException | UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

public static <T> T unmarshalObject(final InputStream input, final Class<T> clazz) throws JAXBException {
    Unmarshaller unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller();
    return (T) unmarshaller.unmarshal(input);
}