Java XML JAXB Unmarshaller unmarshalByContent(Class clazz, String xmlContent)

Here you can find the source of unmarshalByContent(Class clazz, String xmlContent)

Description

unmarshal By Content

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
public static <T> T unmarshalByContent(Class<T> clazz, String xmlContent) 

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 {

    @SuppressWarnings("unchecked")
    public static <T> T unmarshalByContent(Class<T> clazz, String xmlContent) {
        Reader reader = null;//ww w  .j  av  a  2s. c  om
        try {
            reader = new StringReader(xmlContent);
            JAXBContext context = JAXBContext.newInstance(clazz);
            return (T) context.createUnmarshaller().unmarshal(reader);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Failed to unmarshal object for class:" + clazz + " xmlContent:" + xmlContent, e);
        } finally {
            Closeables.closeQuietly(reader);
        }
    }

    public static <T> T unmarshal(Class<T> clazz, String xmlInClassPath) {
        Reader reader = null;
        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. unmarshal(String string, Class clazz)
  2. unmarshal(String xml, Class c)
  3. unmarshal(String xml, Class clazz)
  4. unmarshal(String xml, Class clazz)
  5. unmarshal(T entity)
  6. unmarshalDOMElement(byte[] input)
  7. unmarshalFromReader(Reader reader, Class c)
  8. unmarshalFromString(Class clz, String input)
  9. unmarshalFromXml(Class clazz, String xml)