/java/

Unmarshal Service

2014-03-22 19:30:09


<dependency>
<groupid>javax.xml.bind</groupid>
<artifactid>jaxb-api</artifactid>
<version>2.1</version>
</dependency>

package pl.btbw.service;

import pl.btbw.dto.WordsDto;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

public class UnmarshalService

<t> {

    public <t> T getObj(String xmlFilePath, Class<t> clazz) {
        try {
            JAXBContext jc = JAXBContext.newInstance(WordsDto.class);
            Unmarshaller u = jc.createUnmarshaller();

            File f = new File(xmlFilePath);
            return (T)u.unmarshal(f);

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