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

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

Description

xml To Java Bean

License

Open Source License

Parameter

Parameter Description
xml a parameter
c a parameter

Declaration

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

Method Source Code


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

import java.io.StringReader;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Unmarshaller;

public class Main {
    /**/*w  w w  . j  av a 2  s.  c  o  m*/
     * 
     * @param xml
     * @param c
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T xmlToJavaBean(String xml, Class<T> c) {
        if (xml.trim().isEmpty()) {
            return null;
        }
        T t = null;
        try {
            JAXBContext context = JAXBContext.newInstance(c);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            t = (T) unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            e.printStackTrace();
        }

        return t;
    }
}

Related

  1. toObject(String xml, Class c)
  2. toObject(String xml, Class clazz)
  3. xml2obj(final Class cls, final String xml)
  4. xml2Object(String content, Class valueType)
  5. Xml2Object(String xmlString, Class clazz)
  6. xmlToJaxb(Class xmlClass, String xml)
  7. xmlToJaxb(Class xmlClass, String xml)
  8. xmlToObject(String xml, Class... type)
  9. xmlToObject(String xml, Class classe)