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:com.netsteadfast.greenstep.util.ExportData2CsvUtils.java

private static ExportDataConfig getConfig(String configXmlFile) throws Exception {
    InputStream is = ExportData2CsvUtils.class.getClassLoader().getResource(META_CONF_DIR + "/" + configXmlFile)
            .openStream();//  w  ww . ja va 2s. com
    byte[] xmlContent = IOUtils.toString(is, Constants.BASE_ENCODING).getBytes();
    JAXBContext jaxbContext = JAXBContext.newInstance(ExportDataConfig.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    ByteArrayInputStream bais = new ByteArrayInputStream(xmlContent);
    ExportDataConfig config = (ExportDataConfig) jaxbUnmarshaller.unmarshal(bais);
    return config;
}

From source file:cn.newtouch.util.utils.encode.JaxbBinder.java

/**
 * @param types ??Root.//from   ww  w. j  a  v  a  2 s  . c o m
 */
public JaxbBinder(Class<?>... types) {
    try {
        jaxbContext = JAXBContext.newInstance(types);
    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.labs64.utils.swid.support.JAXBUtils.java

public static <T> T readObjectFromInputStream(final InputStream inputStream, final Class<T> expectedType) {
    try {//from w  ww  .jav  a2s .c  o  m
        JAXBContext jaxbContext = JAXBContext.newInstance(expectedType);
        Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
        JAXBElement<T> element = unmarshaller.unmarshal(new StreamSource(inputStream), expectedType);
        return element.getValue();
    } catch (final JAXBException e) {
        throw new SwidException("Cannot process resource.", e);
    }
}

From source file:com.impetus.ankush.agent.utils.XMLUtils.java

/**
 * Method to get an XML Object for the given class type from the provided
 * XML configuration file ./* w  w w. j  a v  a 2s  .c om*/
 * 
 * @param <S>
 *            The class name for which XML object needs to be returned.
 * @param filePath
 *            File Path of XML file.
 * @param className
 *            Class name of returning object.
 * @return The Object of provided class
 * @throws Exception
 */
public static <S> S getXMLObject(String filePath, Class<S> className) throws Exception {
    // java XML context object.
    JAXBContext jc = JAXBContext.newInstance(className);
    // file.
    File file = new File(filePath);
    if (file.exists()) {
        // if file exists read the file content.
        String fileContent = FileUtils.readFileToString(file);
        // if not empty then unmarshal the object.
        if (!fileContent.isEmpty()) {
            // Creating unmarshaller
            Unmarshaller unmarshaller = jc.createUnmarshaller();
            // Getting component services
            S object = (S) unmarshaller.unmarshal(file);
            // returning object.
            return object;
        }
    }
    return null;
}

From source file:fish.payara.examples.jdays2016.springboot.DataLoaderServiceImpl.java

@Override
public long parseFile(File file) {
    JAXBContext jc;//from w w  w.  jav  a 2  s  .c  o m
    int count = 0;
    try {
        jc = JAXBContext.newInstance("fish.payara.examples.jdays2016.jaxb");
        Unmarshaller um = jc.createUnmarshaller();
        HaPlannedRoadworks rw = (HaPlannedRoadworks) um.unmarshal(file);
        for (HaPlannedRoadworks.HaPlannedWorks works : rw.getHaPlannedWorks()) {
            PlannedWorks pw = new PlannedWorks(works);
            repo.save(pw);
            count++;
        }
    } catch (JAXBException ex) {
        Logger.getLogger(DataLoaderServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    return count;

}

From source file:com.framework.infrastructure.mapper.JaxbMapper.java

/**
 * @param rootTypes RootClass.//from  w w w.  j a v a2 s  .c  om
 */
public JaxbMapper(Class<?>... rootTypes) {
    try {
        jaxbContext = JAXBContext.newInstance(rootTypes);
    } catch (JAXBException e) {
        Exceptions.unchecked(e);
    }
}

From source file:com.redhat.akashche.wixgen.jaxb.SampleInstallerTest.java

@Test
public void test() throws Exception {
    JAXBContext jaxb = JAXBContext.newInstance(SampleInstallerTest.class.getPackage().getName());
    InputStream is = null;//ww w  . ja v a 2s.c om
    Writer writer = null;
    try {
        is = SampleInstallerTest.class.getResourceAsStream("SampleInstaller.wxs");
        Reader reader = new InputStreamReader(is, Charset.forName("UTF-8"));
        Wix wix = (Wix) jaxb.createUnmarshaller().unmarshal(reader);
        wix.getProduct().setName(wix.getProduct().getName() + " (JAXB)");
        //            OutputStream os = new FileOutputStream("SampleInstaller_JAXB.wxs");
        OutputStream os = new NullOutputStream();
        writer = new OutputStreamWriter(os, Charset.forName("UTF-8"));
        Marshaller marshaller = jaxb.createMarshaller();
        marshaller.setProperty(JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(wix, writer);
    } finally {
        closeQuietly(is);
        closeQuietly(writer);
    }
}

From source file:eu.openminted.toolkit.elsevier.retriever.JaxbMarshalingTest.java

@Before
public void initContext() {
    try {/*w w  w.  j av  a  2  s.co  m*/
        jc = JAXBContext.newInstance(FullTextRetrievalResponse.class);
    } catch (JAXBException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

private static synchronized JAXBContext getCachedContext(String pkg) throws JAXBException {
    JAXBContext ctx = _ctxMap.get(pkg);
    if (ctx == null) {
        ctx = JAXBContext.newInstance(pkg);
        _ctxMap.put(pkg, ctx);//from   w  ww .j a  v  a2  s  .  c o m
    }
    return ctx;
}

From source file:com.mycompany.dao.report.XMLReport.java

@Override
public void generate() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Books.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    Books books = (Books) jaxbUnmarshaller.unmarshal(new File(fileName));

    for (Iterator<Book> iterator = books.getBooks().iterator(); iterator.hasNext();) {
        Book book = iterator.next();
        if (book.getQuantity() != 0) {
            // Remove the current element from the iterator and the list.
            iterator.remove();// www . j ava  2s .c  om
        }
    }

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    jaxbMarshaller.marshal(books, new File(fileName2));

}