Here you can find the source of fromFile(Class jaxbClass, String fileName)
Parameter | Description |
---|---|
jaxbClass | a parameter |
fileName | of the file to read to construct the object |
Parameter | Description |
---|---|
Exception | an exception |
public static Object fromFile(Class jaxbClass, String fileName) throws Exception
//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)); } }