Java XML JAXB String to Object fromXML(String xml, Class valueType)

Here you can find the source of fromXML(String xml, Class valueType)

Description

from XML

License

Apache License

Declaration

@SuppressWarnings({ "unchecked", "restriction" })
    public static <T> T fromXML(String xml, Class<T> valueType) 

Method Source Code


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

import java.io.StringReader;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Unmarshaller;

public class Main {
    @SuppressWarnings({ "unchecked", "restriction" })
    public static <T> T fromXML(String xml, Class<T> valueType) {
        try {/*from  w w  w.  j  a va  2  s  . c  o  m*/
            JAXBContext context = JAXBContext.newInstance(valueType);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            return (T) unmarshaller.unmarshal(new StringReader(xml));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        }
    }
}

Related

  1. fromXml(InputStream xml, Class objectClass)
  2. fromXml(Reader xml, Class clazz)
  3. fromXml(String responseBody, Class c)
  4. fromXml(String xml, Class clazz)
  5. fromXml(String xml, Class type)
  6. getXmlObject(String xml, Class cls)
  7. stringToObject(String s, Class theclass)
  8. toObject(Class className, String strXml)
  9. toObject(String xml, Class c)