Here you can find the source of unmarshal(String content, Class
Parameter | Description |
---|---|
content | the string containing the text to unmarshal. |
clasz | the class resulting from the unmarshal process. |
Parameter | Description |
---|---|
Exception | if an error occurs. |
@SuppressWarnings({ "rawtypes", "unchecked" }) synchronized public static <T> T unmarshal(String content, Class<T> clasz) throws Exception
//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(); } }