Java XML JAXB Unmarshaller unmarshal(Class clazz, String xmlInClassPath)

Here you can find the source of unmarshal(Class clazz, String xmlInClassPath)

Description

unmarshal

License

Open Source License

Declaration

public static <T> T unmarshal(Class<T> clazz, String xmlInClassPath) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import com.google.common.io.Closeables;
import com.google.common.io.Resources;
import javax.xml.bind.JAXBContext;

import java.io.*;
import java.net.URL;

public class Main {

    public static <T> T unmarshal(Class<T> clazz, String xmlInClassPath) {
        Reader reader = null;//  www  . j a va 2s . c  o m
        try {
            URL xmlUrl = Resources.getResource(xmlInClassPath);
            reader = new InputStreamReader(xmlUrl.openStream(), "UTF-8");
            JAXBContext context = JAXBContext.newInstance(clazz);
            return (T) context.createUnmarshaller().unmarshal(reader);
        } catch (Exception e) {
            throw new RuntimeException("Failed to unmarshal object for class:" + clazz + " xml:" + xmlInClassPath,
                    e);
        } finally {
            Closeables.closeQuietly(reader);
        }
    }
}

Related

  1. marshallUnmarshall(T inObj)
  2. unmarshal(Class beanClass, InputStream is)
  3. unmarshal(Class c, Object o)
  4. unmarshal(Class clazz, Source source)
  5. unmarshal(Class clazz, String xml)
  6. unmarshal(Class clz, File file)
  7. unmarshal(File f)
  8. unmarshal(final Class clazz, String json)
  9. unmarshal(final String xml, Class clazz, InputStream inputSchema)