Java XML JAXB Unmarshaller unmarshal(String content, Class clasz)

Here you can find the source of unmarshal(String content, Class clasz)

Description

Unmarshal class.

License

Open Source License

Parameter

Parameter Description
content the string containing the text to unmarshal.
clasz the class resulting from the unmarshal process.

Exception

Parameter Description
Exception if an error occurs.

Return

the unmarshalled object.

Declaration

@SuppressWarnings({ "rawtypes", "unchecked" })
synchronized public static <T> T unmarshal(String content, Class<T> clasz) throws Exception 

Method Source Code

//package com.java2s;
/**/*  w  w  w . j  a v  a2  s .  com*/
 * This file is part of JEMMA - http://jemma.energy-home.org
 * (C) Copyright 2013 Telecom Italia (http://www.telecomitalia.it)
 *
 * JEMMA is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License (LGPL) version 3
 * or later as published by the Free Software Foundation, which accompanies
 * this distribution and is available at http://www.gnu.org/licenses/lgpl.html
 *
 * JEMMA 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 (LGPL) for more details.
 *
 */

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;

import javax.xml.bind.Unmarshaller;

import javax.xml.transform.stream.StreamSource;

public class Main {
    /**
     * Unmarshal class.
     * 
     * @param content
     *            the string containing the text to unmarshal.
     * @param clasz
     *            the class resulting from the unmarshal process.
     * @return the unmarshalled object.
     * @throws Exception
     *             if an error occurs.
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    synchronized public static <T> T unmarshal(String content, Class<T> clasz) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(clasz);
        Unmarshaller u = jc.createUnmarshaller();
        Object o = null;
        byte[] _res = null;
        _res = content.getBytes("UTF-8");
        String __str = "";
        __str = new String(_res, "UTF-8");
        StringBuffer xmlStr = new StringBuffer((!__str.startsWith("<") ? __str.substring(3) : __str));
        o = u.unmarshal(new StreamSource(new StringReader(xmlStr.toString())), clasz);
        return (T) ((JAXBElement) o).getValue();
    }
}

Related

  1. unmarshal(JAXBContext context, Class clazz, String source)
  2. unmarshal(JAXBContext jaxbContext, XMLStreamReader xmlStreamReader)
  3. unmarshal(org.w3c.dom.Element elem, Class c)
  4. unmarshal(org.w3c.dom.Element elem, Class c)
  5. unmarshal(String b, Class implClass, Class... bc)
  6. unMarshal(String contextPath, InputStream xmlStream)
  7. unmarshal(String ObjXml, Class configurationClass)
  8. unmarshal(String packageName, InputStream inputStream)
  9. unmarshal(String string, Class clazz)