Java XML JAXB String to Object Xml2Object(String xmlString, Class clazz)

Here you can find the source of Xml2Object(String xmlString, Class clazz)

Description

Xml Object

License

Open Source License

Declaration

public static Object Xml2Object(String xmlString, Class<?> clazz) throws JAXBException 

Method Source Code


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

import java.io.StringReader;

import java.net.URL;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

public class Main {
    public static Object Xml2Object(String xmlString, Class<?> clazz) throws JAXBException {
        if (xmlString == null || "".equals(xmlString.trim())) {
            return null;
        }/*w ww.  j av  a  2s .c om*/
        Unmarshaller unmarshaller = createUnMarshallerByClazz(clazz);
        return unmarshaller.unmarshal(new StringReader(xmlString));
    }

    public static Object Xml2Object(URL url, Class<?> clazz) throws JAXBException {
        if (url == null) {
            return null;
        }
        Unmarshaller unmarshaller = createUnMarshallerByClazz(clazz);
        return unmarshaller.unmarshal(url);
    }

    public static Unmarshaller createUnMarshallerByClazz(Class<?> clazz) throws JAXBException {
        JAXBContext jax = JAXBContext.newInstance(clazz);
        Unmarshaller unMar = jax.createUnmarshaller();

        return unMar;
    }
}

Related

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