Unmarshall to Object via JAXB - Java XML

Java examples for XML:JAXB

Description

Unmarshall to Object via JAXB

Demo Code


//package com.java2s;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        String name = "java2s.com";
        System.out.println(load(clazz, name));
    }/*  w w  w  . ja  v a2s .com*/

    public final static Object load(Class clazz, String name) {
        try {
            File file = new File(name);
            JAXBContext jaxbContext = JAXBContext.newInstance(clazz);
            Unmarshaller jaxbUnmarshaller = jaxbContext
                    .createUnmarshaller();
            return jaxbUnmarshaller.unmarshal(file);
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }
}

Related Tutorials