Java XML JAXB Unmarshaller unmarshal(final String xml, final Class clazz)

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

Description

JAXBContext.newInstance(..) is a slow operation.

License

Open Source License

Declaration

public static <T extends Object> T unmarshal(final String xml, final Class<T> clazz) throws JAXBException 

Method Source Code


//package com.java2s;
/*/*from  w  ww . j a va2  s .c o m*/
 * Copyright (2009) Schibsted ASA
 *   This file is part of Sesat Commons.
 *
 *   Sesat Commons is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU Lesser General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   Sesat Commons is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Lesser General Public License for more details.
 *
 *   You should have received a copy of the GNU Lesser General Public License
 *   along with Sesat Commons.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.io.StringReader;

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

public class Main {
    /** JAXBContext.newInstance(..) is a slow operation.
     * @deprecated It is better the client creates the JAXBContent once and re-uses it to the other unmarshal method.
     */
    public static <T extends Object> T unmarshal(final String xml, final Class<T> clazz) throws JAXBException {

        return (T) unmarshal(JAXBContext.newInstance(clazz), xml);
    }

    public static Object unmarshal(final JAXBContext context, final String xml) throws JAXBException {

        return context.createUnmarshaller().unmarshal(new StringReader(xml));
    }
}

Related

  1. unmarshal(Class clazz, String xmlInClassPath)
  2. unmarshal(Class clz, File file)
  3. unmarshal(File f)
  4. unmarshal(final Class clazz, String json)
  5. unmarshal(final String xml, Class clazz, InputStream inputSchema)
  6. unmarshal(InputSource inputSource, Class clazz)
  7. unmarshal(InputStream content, Class expectedType)
  8. unmarshal(InputStream in, Class... boundClasses)
  9. unmarshal(InputStream inputStream, Class entityClass)