unmarshal via JAXB - Java XML

Java examples for XML:JAXB

Description

unmarshal via JAXB

Demo Code


//package com.java2s;
import java.io.InputStream;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;

public class Main {
    public static <T> T unmarshal(Source source, Class<T> c) {
        try {//from   w  w w.j ava2s  . c o  m
            JAXBContext jc = JAXBContext.newInstance(c);
            Unmarshaller um = jc.createUnmarshaller();
            return um.unmarshal(source, c).getValue();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    public static <T> T unmarshal(InputStream is, Class<T> c) {
        Source source = new StreamSource(is);
        return unmarshal(source, c);
    }
}

Related Tutorials