Java XML JAXB String to Object converyToJavaBean(String xml, Class c)

Here you can find the source of converyToJavaBean(String xml, Class c)

Description

convery To Java Bean

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
public static <T> T converyToJavaBean(String xml, Class<T> c) 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Unmarshaller;

public class Main {

    @SuppressWarnings("unchecked")
    public static <T> T converyToJavaBean(String xml, Class<T> c) {
        T t = null;/*from   ww  w .  j a  v  a  2s . c o  m*/
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return t;
    }

    @SuppressWarnings("unchecked")
    public static <T> T converyToJavaBean(File xmlFile, Class<T> c) {
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(xmlFile);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return t;
    }
}

Related

  1. convertToXmlFile(File file, Object source, Class... type)
  2. convertXmlFileToObject(Class clazz, String xmlPath)
  3. convertXmlToObj(Class clazz, String xmlStr)
  4. convertXMLToObject(byte[] data, Class clazz)
  5. converyToJavaBean(String xml, Class c)
  6. converyToJavaBean(String xml, Class clazz)
  7. createObject(String xml, Object type)
  8. fromFile(Class jaxbClass, String fileName)
  9. fromStream(Class jaxbClass, InputStream is)