Java XML JAXB String to Object fromStream(Class jaxbClass, InputStream is)

Here you can find the source of fromStream(Class jaxbClass, InputStream is)

Description

fromStream retrieves object of given class from given inputstream

License

Educational Community License

Parameter

Parameter Description
jaxbClass a parameter
is stream to read to construct the object

Exception

Parameter Description
Exception an exception

Declaration

public static Object fromStream(Class jaxbClass, InputStream is)
        throws Exception 

Method Source Code

//package com.java2s;
/**//from  w w  w.  java2s .  c om
 *  This document is a part of the source code and related artifacts
 *  for CollectionSpace, an open source collections management system
 *  for museums and related institutions.

 *  http://www.collectionspace.org
 *  http://wiki.collectionspace.org

 *  Copyright 2010 University of California at Berkeley

 *  Licensed under the Educational Community License (ECL), Version 2.0.
 *  You may not use this file except in compliance with this License.

 *  You may obtain a copy of the ECL 2.0 License at

 *  https://source.collectionspace.org/collection-space/LICENSE.txt

 *  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 java.io.InputStream;

import javax.xml.bind.JAXBContext;

import javax.xml.bind.Unmarshaller;

public class Main {
    /**
     * fromStream retrieves object of given class from given inputstream
     * @param jaxbClass
     * @param is stream to read to construct the object
     * @return
     * @throws Exception
     */
    public static Object fromStream(Class jaxbClass, InputStream is)
            throws Exception {
        JAXBContext context = JAXBContext.newInstance(jaxbClass);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        //note: setting schema to null will turn validator off
        unmarshaller.setSchema(null);
        return jaxbClass.cast(unmarshaller.unmarshal(is));
    }
}

Related

  1. converyToJavaBean(String xml, Class c)
  2. converyToJavaBean(String xml, Class c)
  3. converyToJavaBean(String xml, Class clazz)
  4. createObject(String xml, Object type)
  5. fromFile(Class jaxbClass, String fileName)
  6. fromStringToObject(String xmlString, Class xmlObjClass)
  7. fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml)
  8. fromXML(Class clazz, InputStream is)
  9. fromXml(Class tClass, InputStream inputStream)