Here you can find the source of deSerialize(Class
Parameter | Description |
---|---|
docClass | The class of the object to unmarshall the XML as |
inputStream | An input stream containing the XML to deserialize |
T | The type of object to unmarshall the XML as |
Parameter | Description |
---|---|
JAXBException | if an error occurs during deserialization |
public static <T> T deSerialize(Class<T> docClass, InputStream inputStream) throws JAXBException
//package com.java2s; /*//from ww w.jav a2 s . c om * Copyright 2017 Frederic Thevenet * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * 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 javax.xml.bind.JAXB; import javax.xml.bind.JAXBException; import javax.xml.transform.stream.StreamSource; import java.io.*; public class Main { public static <T> T deSerialize(Class<T> docClass, File file) throws JAXBException, IOException { try (FileInputStream fin = new FileInputStream(file)) { return deSerialize(docClass, fin); } } /** * Deserialize the XML content of a stream into a Java object of the specified type. * * @param docClass The class of the object to unmarshall the XML as * @param inputStream An input stream containing the XML to deserialize * @param <T> The type of object to unmarshall the XML as * @return The deserialized object * @throws JAXBException if an error occurs during deserialization */ public static <T> T deSerialize(Class<T> docClass, InputStream inputStream) throws JAXBException { return deSerialize(docClass, new StreamSource(inputStream)); } /** * Deserialize the XML content of a string into a Java object of the specified type. * * @param docClass The class of the object to unmarshall the XML as * @param xmlString The XML to deserialize, as a string * @param <T> The type of object to unmarshall the XML as * @return The deserialized object * @throws JAXBException if an error occurs during deserialization */ public static <T> T deSerialize(Class<T> docClass, String xmlString) throws JAXBException { return deSerialize(docClass, new StreamSource(new StringReader(xmlString))); } private static <T> T deSerialize(Class<T> docClass, StreamSource source) throws JAXBException { return JAXB.unmarshal(source, docClass); } }