Java XML JAXB String to Object fromFile(Class jaxbClass, String fileName)

Here you can find the source of fromFile(Class jaxbClass, String fileName)

Description

fromFile retrieves object of given class from given file (in classpath)

License

Educational Community License

Parameter

Parameter Description
jaxbClass a parameter
fileName of the file to read to construct the object

Exception

Parameter Description
Exception an exception

Declaration

public static Object fromFile(Class jaxbClass, String fileName)
        throws Exception 

Method Source Code

//package com.java2s;
/**// w  w  w.jav  a  2 s .  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 {
    /**
     * fromFile retrieves object of given class from given file (in classpath)
     * @param jaxbClass
     * @param fileName of the file to read to construct the object
     * @return
     * @throws Exception
     */
    public static Object fromFile(Class jaxbClass, String fileName)
            throws Exception {

        JAXBContext context = JAXBContext.newInstance(jaxbClass);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        //note: setting schema to null will turn validator off
        unmarshaller.setSchema(null);
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        InputStream is = tccl.getResourceAsStream(fileName);
        return fromStream(jaxbClass, is);
    }

    /**
     * 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. convertXMLToObject(byte[] data, Class clazz)
  2. converyToJavaBean(String xml, Class c)
  3. converyToJavaBean(String xml, Class c)
  4. converyToJavaBean(String xml, Class clazz)
  5. createObject(String xml, Object type)
  6. fromStream(Class jaxbClass, InputStream is)
  7. fromStringToObject(String xmlString, Class xmlObjClass)
  8. fromXml(@SuppressWarnings("rawtypes") Class clazz, String stringXml)
  9. fromXML(Class clazz, InputStream is)