Java XML JAXB String to Object fromXml(String xml, Class clazz)

Here you can find the source of fromXml(String xml, Class clazz)

Description

from Xml

License

Open Source License

Parameter

Parameter Description
xml a parameter
clazz a parameter

Declaration

@SuppressWarnings("unchecked")
public static <T> T fromXml(String xml, Class<T> clazz) 

Method Source Code

//package com.java2s;
/*//from   w ww .j a  v a 2s.c o  m
* Project Name: xinyunlian-ecom
* File Name: JaxbUtils.java
* Class Name: JaxbUtils
*
* Copyright 2014 Hengtian Software Inc
*
* Licensed under the Hengtiansoft
*
* http://www.hengtiansoft.com
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.io.StringReader;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

public class Main {
    private static ConcurrentMap<Class<?>, JAXBContext> jaxbMap = new ConcurrentHashMap<Class<?>, JAXBContext>();

    /**
     * @param xml
     * @param clazz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T fromXml(String xml, Class<T> clazz) {
        try {
            StringReader reader = new StringReader(xml);
            return (T) createUnmarshaller(clazz).unmarshal(reader);
        } catch (JAXBException e) {
            // LOGGER.error("Error occured when parsing to object.", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @param clazz
     * @return
     */
    public static Unmarshaller createUnmarshaller(Class<?> clazz) {
        try {
            JAXBContext jaxbContext = getJaxbContext(clazz);
            return jaxbContext.createUnmarshaller();
        } catch (JAXBException e) {
            // LOGGER.error("Error occured when creating Unmarshaller.", e);
            throw new RuntimeException(e);
        }
    }

    /**
     * @param clazz
     * @return
     */
    private static JAXBContext getJaxbContext(Class<?> clazz) {
        JAXBContext jaxbContext = jaxbMap.get(clazz);
        if (jaxbContext == null) {
            try {
                jaxbContext = JAXBContext.newInstance(clazz);
                jaxbMap.putIfAbsent(clazz, jaxbContext);
            } catch (JAXBException ex) {
                throw new RuntimeException("Could not instantiate JAXBContext.", ex);
            }
        }
        return jaxbContext;
    }
}

Related

  1. fromXml(final Reader reader, final Class dtoClass)
  2. fromXml(InputStream input, Class clazz)
  3. fromXml(InputStream xml, Class objectClass)
  4. fromXml(Reader xml, Class clazz)
  5. fromXml(String responseBody, Class c)
  6. fromXml(String xml, Class type)
  7. fromXML(String xml, Class valueType)
  8. getXmlObject(String xml, Class cls)
  9. stringToObject(String s, Class theclass)