List of usage examples for javax.xml.bind JAXBContext newInstance
public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T xmlToBean(InputStream input, Class<T> t) { try {/*from www . ja v a 2 s. c o m*/ JAXBContext context = JAXBContext.newInstance(t); Unmarshaller unmarshaller = context.createUnmarshaller(); T ts = (T) unmarshaller.unmarshal(new InputStreamReader(input, "UTF-8")); return ts; } catch (JAXBException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return null; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXML(Class<?> class1, File file) throws JAXBException, IOException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (FileReader reader = new FileReader(file)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }// ww w . ja v a 2 s. c o m }
From source file:Main.java
/** * Marshal a JAXB element to a XML file/*from w ww.jav a 2s . c o m*/ * * @param jaxbElement * The root of content tree to be marshalled * @param strXMLFilePath * XML output file path * @throws Exception * in error case */ public static void doMarshalling(Object jaxbElement, String strXMLFilePath) throws Exception { if (jaxbElement == null) { throw new RuntimeException("No JAXB element to marshal (null)!"); } if (strXMLFilePath == null) { throw new RuntimeException("No XML file path (null)!"); } FileOutputStream fos = null; try { JAXBContext jaxbCtx = JAXBContext.newInstance(jaxbElement.getClass().getPackage().getName()); Marshaller marshaller = jaxbCtx.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); // NOI18N marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); fos = new FileOutputStream(strXMLFilePath); marshaller.marshal(jaxbElement, fos);// System.out); } catch (Exception e) { // Logger.XMLEval.logState("Marshalling failed: " + e.getMessage(), LogLevel.Error); throw e; } finally { if (fos != null) { fos.close(); fos = null; } } }
From source file:Main.java
private static Marshaller createMarshall(String pkgName) throws JAXBException { JAXBContext jaxbCtx = null;//from w ww . j ava2s . c om if ((jaxbCtx = marshallContexts.get(pkgName)) == null) { jaxbCtx = JAXBContext.newInstance(pkgName); marshallContexts.put(pkgName, jaxbCtx); } Marshaller marshaller = jaxbCtx.createMarshaller(); return marshaller; }
From source file:Main.java
public static <T> T UnMarshall(Class<T> objectClass, XMLStreamReader reader) throws JAXBException { JAXBContext jc = JAXBContext.newInstance(objectClass); Unmarshaller u = jc.createUnmarshaller(); return objectClass.cast(u.unmarshal(reader)); }
From source file:Main.java
private static <T> JAXBContext createJAXBContext(Class<T> clazz) throws JAXBException { return JAXBContext.newInstance(clazz); }
From source file:Main.java
public static <T extends Object> void marshal(Class clz, T marshalObj) throws JAXBException { JAXBContext context = JAXBContext.newInstance(clz); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(marshalObj, System.out); }
From source file:Main.java
/** * Converts the XML file specified into the specified POJO type * @param <T> the object type of the POJO * @param xmlfile the XML file to convert * @param classOfT the class of the POJO * @return the POJO object if conversion was successful * @throws JAXBException/*from www.ja v a 2s .c o m*/ * @throws XMLStreamException * @throws FileNotFoundException */ public static <T> T convertToPojo(File xmlfile, Class<T> classOfT) throws JAXBException, XMLStreamException, FileNotFoundException { JAXBContext jaxbContext = JAXBContext.newInstance(classOfT); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); XMLInputFactory xif = XMLInputFactory.newFactory(); // settings to prevent xxe // would be funny if this tool is itsef is vulnerable to xxe :D xif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); xif.setProperty(XMLInputFactory.SUPPORT_DTD, false); XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader(xmlfile)); T t = (T) jaxbUnmarshaller.unmarshal(xsr);//(xmlfile); return t; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T unmarshal(Class<T> clazz, InputStream input) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); return (T) jaxbUnmarshaller.unmarshal(input); }
From source file:Main.java
public static final Object xml2obj(final Class<?> cls, final String xml) throws JAXBException { StringReader reader = new StringReader(xml); JAXBContext contextIn = JAXBContext.newInstance(cls); Unmarshaller marshallerIn = contextIn.createUnmarshaller(); return marshallerIn.unmarshal(reader); }