List of usage examples for javax.xml.bind Unmarshaller unmarshal
public Object unmarshal(javax.xml.stream.XMLEventReader reader) throws JAXBException;
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> Object xmlToObject(InputStream xmlContent, Class<T> clazz) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setSchema(null); //note: setting schema to null will turn validator off Object unMarshalled = unmarshaller.unmarshal(xmlContent); Object xmlObject = clazz.cast(unMarshalled); return (T) xmlObject; }
From source file:Main.java
/** * @param file/*from w w w. j a va2s .co m*/ * @return * @throws JAXBException */ public static Object unmarshalXML(Class clasz, InputStream file) throws JAXBException { //1. We need to create JAXContext instance JAXBContext jaxbContext = JAXBContext.newInstance(clasz); //2. Use JAXBContext instance to create the Unmarshaller. Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); //3. Use the Unmarshaller to unmarshal the XML document to get an instance of JAXBElement. return (file != null) ? unmarshaller.unmarshal(file) : null; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> Object xmlToObject(String xmlContent, Class<T> clazz) throws JAXBException { ByteArrayInputStream xmlContentBytes = new ByteArrayInputStream(xmlContent.getBytes()); JAXBContext context = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = context.createUnmarshaller(); unmarshaller.setSchema(null); //note: setting schema to null will turn validator off Object unmarshalledObj = unmarshaller.unmarshal(xmlContentBytes); Object xmlObject = clazz.cast(unmarshalledObj); return (T) xmlObject; }
From source file:com.u2apple.tool.filter.ExcludeFilter.java
private static void loadRules() { URL url = DevicePatternFilter.class.getResource(RULES_CONF); File file = new File(url.getFile()); JAXBContext jaxbContext;/*from w ww.j av a 2 s . com*/ try { jaxbContext = JAXBContext.newInstance(Rules.class); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); rules = (Rules) unmarshaller.unmarshal(file); } catch (JAXBException ex) { logger.error("Fail to parse exclude-filter.xml as {}", ex); } }
From source file:eu.planets_project.tb.impl.serialization.ExperimentRecords.java
/** * @param in/* w w w . java 2s. co m*/ * @return */ public static ExperimentRecords readFromInputStream(InputStream in) { try { JAXBContext jc = JAXBContext.newInstance(ExperimentRecords.class); Unmarshaller u = jc.createUnmarshaller(); ExperimentRecords exp = (ExperimentRecords) u.unmarshal(in); return exp; } catch (JAXBException e) { log.fatal("Reading Experiments from XML failed: " + e); return null; } }
From source file:att.jaxrs.util.Marshal.java
public static <T> T unmarshal(Class<T> xmlType, InputStream inputStream) { System.out.println("unmarshalling input stream"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); StringBuilder out = new StringBuilder(); try {/*from www . j a va2s. c om*/ String line; while ((line = reader.readLine()) != null) { out.append(line); } } catch (IOException e) { System.out.println(e.getMessage()); } String namespace = out.toString(); namespace = namespace.replaceAll(Constants.DATA_SERVICE_XMLNS, ""); InputStream stream = Util.getInputStreamFromString(namespace); JAXBContext jaxbContext; XmlRootElement doc = null; try { jaxbContext = JAXBContext.newInstance(xmlType); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); doc = (XmlRootElement) unmarshaller.unmarshal(stream); System.out.println("unmarshall successful"); } catch (JAXBException e) { e.printStackTrace(); } return (T) doc; }
From source file:com.netsteadfast.greenstep.util.DynamicHqlUtils.java
public static DynamicHql loadResource(String resource) throws Exception { DynamicHql dynamicHql = resourceDataMap.get(resource); if (dynamicHql == null) { InputStream in = null;// w ww .j av a 2s . co m try { in = DynamicHqlUtils.class.getResourceAsStream("/dynamichql/" + resource); byte[] xmlBytes = IOUtils.toByteArray(in); JAXBContext jaxbContext = JAXBContext.newInstance(DynamicHql.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); dynamicHql = (DynamicHql) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(xmlBytes)); resourceDataMap.put(resource, dynamicHql); } catch (IOException e) { logger.error(e.getMessage().toString()); throw e; } finally { if (in != null) { IOUtils.closeQuietly(in); } in = null; } } return dynamicHql; }
From source file:eu.hydrologis.jgrass.featureeditor.utils.Utilities.java
/** * Parse a form xml.// w w w . j av a 2 s .co m * * @param xml the xml containing the form definition. * @return the {@link AForm}. * @throws Exception */ public static AForm parseXML(String xml) throws Exception { JAXBContext jc = JAXBContext.newInstance("eu.hydrologis.jgrass.featureeditor.xml.annotated"); //$NON-NLS-1$ Unmarshaller um = jc.createUnmarshaller(); StringReader sr = new StringReader(xml); return (AForm) um.unmarshal(sr); }
From source file:Main.java
/** * Reads an XML file and returns the content as an object of the specified class. * @param file XML file.// w ww.ja v a 2 s. co m * @param typeParameterClass Class of the main object in the XML file. * @return Content as an object of the specified class. */ @SuppressWarnings("unchecked") public static <T> T read(File file, Class<T> typeParameterClass) { T content = null; try { JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //jaxbUnmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); content = (T) jaxbUnmarshaller.unmarshal(file); } catch (JAXBException e) { e.printStackTrace(); } return content; }
From source file:com.impetus.ankush.agent.utils.XMLUtils.java
/** * Method to get an XML Object for the given class type from the provided * XML configuration file .//from w w w. j ava 2 s. com * * @param <S> * The class name for which XML object needs to be returned. * @param filePath * File Path of XML file. * @param className * Class name of returning object. * @return The Object of provided class * @throws Exception */ public static <S> S getXMLObject(String filePath, Class<S> className) throws Exception { // java XML context object. JAXBContext jc = JAXBContext.newInstance(className); // file. File file = new File(filePath); if (file.exists()) { // if file exists read the file content. String fileContent = FileUtils.readFileToString(file); // if not empty then unmarshal the object. if (!fileContent.isEmpty()) { // Creating unmarshaller Unmarshaller unmarshaller = jc.createUnmarshaller(); // Getting component services S object = (S) unmarshaller.unmarshal(file); // returning object. return object; } } return null; }