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

/**
 * Saves the data in the file in xml format.
 *
 * @param file Points to a valid xml file containing data that match the {@code classToConvert}.
 *             Cannot be null./*from w  w w. j  a  v a 2  s .  c  om*/
 * @throws FileNotFoundException Thrown if the file is missing.
 * @throws JAXBException         Thrown if there is an error during converting the data
 *                               into xml and writing to the file.
 */
public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException {

    assert file != null;
    assert data != null;

    if (!file.exists()) {
        throw new FileNotFoundException("File not found : " + file.getAbsolutePath());
    }

    JAXBContext context = JAXBContext.newInstance(data.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    m.marshal(data, file);
}

From source file:Main.java

public static <T extends Object> T unmarshalFromString(Class clz, String input) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(clz);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    T unobj = (T) unmarshaller.unmarshal(new StreamSource(new StringReader(input.toString())));
    return unobj;
}

From source file:Main.java

/**
 * Prints the document for debug./*w  ww .j  av a  2s  .  com*/
 *
 * @param model the model to be printed
 * @throws Exception for any errors encountered
 */
public static void printModel(Object model) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance("gov.medicaid.domain.model");
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(model, System.out);
}

From source file:Main.java

public static Document deserialize(Object object) throws Exception {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);//from  w  ww. jav a 2  s.  com
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.newDocument();

    JAXBContext context = JAXBContext.newInstance(object.getClass());
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    m.marshal(object, doc);
    return doc;
}

From source file:Main.java

public static Object xmlToJaxb(Class<?> xmlClass, InputStream is) throws JAXBException, IOException {
    JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    JAXBElement<?> element = (JAXBElement<?>) unmarshaller.unmarshal(getReader(is));
    return element.getValue();
}

From source file:Main.java

public static <T> void saveObject(T object, Class<T> typeClass, String path) {
    try {//from  w  w  w. ja  v  a 2s .co  m
        File file = new File(path);
        JAXBContext jaxbContext = JAXBContext.newInstance(typeClass);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        jaxbMarshaller.marshal(object, file);

    } catch (JAXBException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

/**
 * Convert XML data to a bean using JAXB.
 * //  w  w  w.ja v  a 2  s. c o  m
 * @param b
 *            The bean, represented as XML.
 * @param implClass
 *            The implementation class of the bean.
 * @param bc
 *            Additional classes to add to the JAXB context.
 * @return The bean, unmarshalled from the XML data using JAXB.
 */
public static <T> T unmarshal(String b, Class<T> implClass, Class<?>... bc) {
    Class<?>[] bind;
    if (bc.length > 1) {
        bind = new Class<?>[bc.length + 1];
        bind[0] = implClass;
        for (int i = 0; i < bc.length; i++) {
            bind[i + 1] = bc[i];
        }
    } else {
        bind = new Class<?>[] { implClass, };
    }
    ByteArrayInputStream bais = new ByteArrayInputStream(b.getBytes());
    try {
        JAXBContext jaxbContext = JAXBContext.newInstance(bind);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        return implClass.cast(unmarshaller.unmarshal(bais));
    } catch (JAXBException e) {
        throw new IllegalStateException("Error unmarshalling", e);
    }
}

From source file:Main.java

private static JAXBContext initContext() {
    try {//from w w  w  .  j  a  v  a  2 s. c o m
        return JAXBContext.newInstance("com.tda.sitefilm.allocine.jaxb");
    } catch (JAXBException error) {
        throw new Error("XMLAllocineAPIHelper: Got error during initialization", error);
    }
}

From source file:Main.java

public static Unmarshaller getUnmarshaller() {
    if (unmarshaller != null) {
        return unmarshaller;
    }/*from   w ww. j  a va 2  s  .c  o m*/
    try {
        if (jc == null) {
            jc = JAXBContext.newInstance(REQ_PKG);
        }
        unmarshaller = jc.createUnmarshaller();
        return unmarshaller;
    } catch (JAXBException e) {
        e.printStackTrace();
        return null;
    }
}

From source file:Main.java

/**
 *
 * @param obj//  w w w  .  j  a va  2  s .  co m
 * @throws JAXBException
 */
public static void serializeToSTD(Object obj) throws JAXBException {
    final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
    m.setProperty(JAXB_FRAGMENT, TRUE);
    m.marshal(obj, out);
}