Java XML JAXB Unmarshaller unmarshallXml(final String string, final Class type)

Here you can find the source of unmarshallXml(final String string, final Class type)

Description

unmarshall Xml

License

Open Source License

Declaration

static <OBJECT_TYPE> OBJECT_TYPE unmarshallXml(final String string, final Class<OBJECT_TYPE> type) 

Method Source Code


//package com.java2s;
import java.io.StringReader;

import java.util.Map;
import java.util.Map.Entry;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;

import javax.xml.bind.Unmarshaller;

public class Main {
    static <OBJECT_TYPE> OBJECT_TYPE unmarshallXml(final String string, final Class<OBJECT_TYPE> type) {
        return unmarschall(string, type, null);
    }/* ww w  . j a va  2 s . c  o  m*/

    private static <OBJECT_TYPE> OBJECT_TYPE unmarschall(final String stringRepresentation,
            final Class<OBJECT_TYPE> type, final Map<String, Object> unmarchallerProps) {
        try {
            return unmarschallImpl(stringRepresentation, type, unmarchallerProps);
        } catch (final JAXBException e) {
            throw new RuntimeException(e);
        }
    }

    @SuppressWarnings("unchecked")
    private static <OBJECT_TYPE> OBJECT_TYPE unmarschallImpl(final String stringRepresentation,
            final Class<OBJECT_TYPE> type, final Map<String, Object> unmarchallerProps) throws JAXBException {

        final JAXBContext context = JAXBContext.newInstance(type);
        final Unmarshaller unmarshaller = context.createUnmarshaller();
        if (unmarchallerProps != null) {
            for (final Entry<String, Object> property : unmarchallerProps.entrySet()) {
                unmarshaller.setProperty(property.getKey(), property.getValue());
            }
        }
        return (OBJECT_TYPE) unmarshaller.unmarshal(new StringReader(stringRepresentation));
    }
}

Related

  1. unmarshaller(Class entityType, InputStream in)
  2. unmarshaller(String xml, Class T)
  3. unmarshallJAXBElement(JAXBElement v)
  4. unMarshallRequest(String xmlString)
  5. unmarshallString(String str, Class c)
  6. unmarshallXMLtoObject(final byte[] xmlObject, final Class objectClass, final URL schemaURL)
  7. unmarshalObject(final InputStream input, final Class clazz)
  8. unmarshalPackage(InputStream pkgStream)
  9. unmarshalXML(String xmlString, Class classType)