List of usage examples for javax.xml.bind JAXBException printStackTrace
public void printStackTrace()
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T xmlToBean(InputStream input, Class<T> t) { try {//from w ww . ja va 2 s . c om 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
public static void marshal(Class<?> klass, Object obj, OutputStream os) throws JAXBException { try {/*from w w w. j a v a 2s . co m*/ context = JAXBContext.newInstance(klass); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); unmarshaller = context.createUnmarshaller(); } catch (JAXBException e) { throw new RuntimeException( "There was a problem creating a JAXBContext object for formatting the object to XML."); } try { marshaller.marshal(obj, os); } catch (JAXBException jaxbe) { jaxbe.printStackTrace(); } }
From source file:Main.java
public static Marshaller createMarshaller(String pack, Schema schema) { JAXBContext jaxbContext = null; try {/*from w w w. jav a 2 s . co m*/ jaxbContext = JAXBContext.newInstance(pack); Marshaller marsh = jaxbContext.createMarshaller(); if (schema != null) { marsh.setSchema(schema); // marsh.setEventHandler( new DefaultValidationEventHandler() { // @Override // public boolean handleEvent( ValidationEvent event ) { // return super.handleEvent( event ); // } // }); } marsh.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marsh.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); return marsh; } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static <T> void saveObject(T object, Class<T> typeClass, String path) { try {/*from w ww . ja va 2 s. com*/ File file = new File(path); JAXBContext jaxbContext = JAXBContext.newInstance(typeClass); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, file); } catch (JAXBException e) { e.printStackTrace(); } }
From source file:Main.java
public static <T> void saveObject(T object, Class<T> typeClass, URL path) { try {/*from w ww . j a va 2s. c o m*/ File file = new File(path.toURI()); JAXBContext jaxbContext = JAXBContext.newInstance(typeClass); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // output pretty printed jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, file); } catch (JAXBException e) { e.printStackTrace(); } catch (URISyntaxException e) { e.printStackTrace(); } }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T read(String input, Class<T> typeParameterClass) { T content = null;/*from w w w . j av a2 s. c o m*/ try { JAXBContext jaxbContext = JAXBContext.newInstance(typeParameterClass); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); //jaxbUnmarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); InputStream is = new ByteArrayInputStream(input.getBytes()); content = (T) jaxbUnmarshaller.unmarshal(is); } catch (JAXBException e) { e.printStackTrace(); } return content; }
From source file:Main.java
/** * Reads an XML file and returns the content as an object of the specified class. * @param file XML file./* ww w . jav a 2s . c o 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:Main.java
/** * XML to Object/*from w ww . ja va 2s . co m*/ * @param <T> * @param clazz * @param reader * @return */ @SuppressWarnings("unchecked") public static <T> T convertToObject(Class<T> clazz, Reader reader) { try { if (!uMap.containsKey(clazz)) { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); uMap.put(clazz, unmarshaller); } return (T) uMap.get(clazz).unmarshal(reader); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
public static String convertBean2Xml(Object obj) throws IOException { String result = null;//from w ww . jav a 2 s.c o m try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); ByteArrayOutputStream baos = new ByteArrayOutputStream(); XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); XMLStreamWriter xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(baos, (String) marshaller.getProperty(Marshaller.JAXB_ENCODING)); xmlStreamWriter.writeStartDocument((String) marshaller.getProperty(Marshaller.JAXB_ENCODING), "1.0"); marshaller.marshal(obj, xmlStreamWriter); xmlStreamWriter.writeEndDocument(); xmlStreamWriter.close(); result = baos.toString("UTF-8"); } catch (JAXBException e) { e.printStackTrace(); return null; } catch (XMLStreamException e) { e.printStackTrace(); return null; } catch (UnsupportedEncodingException e) { e.printStackTrace(); return null; } return result; }
From source file:Main.java
public static <T> String toXml(Class<T> z, Object o) { try {//w w w. j a v a 2s. c o m JAXBContext jc = JAXBContext.newInstance(z); Marshaller ms = jc.createMarshaller(); ms.marshal(o, System.out); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ""; }