parse Object using JAXB XML Binding - Android XML

Android examples for XML:JAXB

Description

parse Object using JAXB XML Binding

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 Object parseObject(Object clazzToParse, File xmlFile) {

        Object clazz = null;/*  www. j  a v  a2 s .c  o m*/
        JAXBContext jaxbContext;
        Unmarshaller jaxbUnmarshaller;

        try {

            jaxbContext = createJaxbContext(clazzToParse);

            jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            clazz = (Object) jaxbUnmarshaller.unmarshal(xmlFile);

        } catch (JAXBException e) {

            new RuntimeException(e);

        }

        return clazz;

    }

    private static JAXBContext createJaxbContext(Object clazzToParse)
            throws JAXBException {
        return JAXBContext.newInstance(clazzToParse.getClass());
    }
}

Related Tutorials