Java XML JAXB String to Object xml2Object(String content, Class valueType)

Here you can find the source of xml2Object(String content, Class valueType)

Description

xml Object

License

Apache License

Declaration

@SuppressWarnings("unchecked")
public static <T> T xml2Object(String content, Class<T> valueType) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.ByteArrayInputStream;

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

import javax.xml.bind.Unmarshaller;

public class Main {

    @SuppressWarnings("unchecked")
    public static <T> T xml2Object(String content, Class<T> valueType) {
        try {//  w w w.  j  ava  2  s  .c  o  m
            JAXBContext jaxbContext = JAXBContext.newInstance(valueType);
            Unmarshaller um = jaxbContext.createUnmarshaller();
            T t = (T) um.unmarshal(new ByteArrayInputStream(content.getBytes()));
            return t;
        } catch (JAXBException e) {
            //System.out.println("JAXB castor failed to convert the metadata to module instance.");
        }
        return null;
    }
}

Related

  1. stringToObject(String s, Class theclass)
  2. toObject(Class className, String strXml)
  3. toObject(String xml, Class c)
  4. toObject(String xml, Class clazz)
  5. xml2obj(final Class cls, final String xml)
  6. Xml2Object(String xmlString, Class clazz)
  7. xmlToJavaBean(String xml, Class c)
  8. xmlToJaxb(Class xmlClass, String xml)
  9. xmlToJaxb(Class xmlClass, String xml)