List of usage examples for javax.xml.bind JAXBContext newInstance
public static JAXBContext newInstance(Class<?>... classesToBeBound) throws JAXBException
From source file:Main.java
public static <T> T fromXml(String responseBody, Class<T> c) throws JAXBException, XMLStreamException { ByteArrayInputStream inputStream = new ByteArrayInputStream(responseBody.getBytes()); JAXBContext jaxbContext = JAXBContext.newInstance(c); XMLStreamReader xsr = xmlInputFactory.createXMLStreamReader(inputStream); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); return (T) jaxbUnmarshaller.unmarshal(xsr); }
From source file:Main.java
public static String marshal(Object object) throws Exception { StringWriter string = new StringWriter(); JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); //$NON-NLS-1$ marshaller.marshal(object, string);/*from ww w . j a v a2 s. c o m*/ return string.toString(); }
From source file:Main.java
/** * Object to XML//from w w w . j a v a 2 s .co m * * @param object * @return */ public static String convertToXML(Object object) { try { System.out.println(mMap.containsKey(object.getClass()) + "---mmap-----"); if (!mMap.containsKey(object.getClass())) { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // marshaller.setProperty(CharacterEscapeHandler.class.getName(), // new CharacterEscapeHandler() { // public void escape(char[] ac, int i, int j, boolean // flag,Writer writer) throws IOException { // writer.write( ac, i, j ); } // }); mMap.put(object.getClass(), marshaller); } System.out.println("----mmap--" + mMap.toString()); StringWriter stringWriter = new StringWriter(); mMap.get(object.getClass()).marshal(object, stringWriter); return stringWriter.getBuffer().toString(); } catch (JAXBException e) { e.printStackTrace(); } return null; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXMLFromString(Class<?> class1, String content) throws JAXBException, FileNotFoundException, 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 (StringReader reader = new StringReader(content)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }//from w w w . j av a 2 s. c o m }
From source file:Main.java
/** * //www . j a va 2 s .c om * @param contextPath * @return Marshaller * @throws JAXBException */ public static Marshaller getMarshaller(final String contextPath) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance(contextPath); final Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); return marshaller; }
From source file:Main.java
/** * Marshal a object of//from ww w . j a v a 2s .c o m * <code>classItem</code> from the xmlResponse * <code>String</code>. * * @param xmlResponse <code>String</code> that represents the object to be * marshal. * @param classItem <code>class</code> of the returns object. * @return a object of <code>classItem</code>. * @throws JAXBException throw trying to marshal. */ public static Object unmarshalFromString(String xmlResponse, Class classItem) throws JAXBException { final JAXBContext jaxbContext = JAXBContext.newInstance(classItem); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); StringReader reader = null; try { reader = new StringReader(xmlResponse); return jaxbUnmarshaller.unmarshal(reader); } finally { if (reader != null) reader.close(); } }
From source file:Main.java
@SuppressWarnings("rawtypes") public static String ObjToXml(Object object, boolean isXmlFormat, Class... classesToBeBound) throws JAXBException, IOException { JAXBContext context = JAXBContext.newInstance(classesToBeBound); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isXmlFormat); m.setSchema(null);/* w w w .j a v a2 s . c o m*/ StringWriter sw = new StringWriter(); m.marshal(object, sw); String result = sw.toString(); sw.close(); return result; }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T unmarshall(InputStream xml, Class<T> cl) throws JAXBException { JAXBContext jaxbCtx = JAXBContext.newInstance(cl); Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller(); return (T) unmarshaller.unmarshal(xml); }
From source file:Main.java
public static <T> void marshal(Object jaxbElement, Class<T> jaxbFactory, OutputStream out) throws JAXBException { if (!(jaxbElement instanceof JAXBElement<?>)) { throw new JAXBException("Must be a instance of JAXBElement<?>"); }//from ww w . j av a2s. co m try { JAXBContext jc = JAXBContext.newInstance(jaxbFactory); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(jaxbElement, out); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } }
From source file:Main.java
/** * @param <T>/*from www . j av a 2 s.c o m*/ * the type we want to convert the XML into * @param c * the class of the parameterized type * @param xml * the instance XML description * @return a deserialization of the XML into an object of type T of class * class <T> * @throws javax.xml.bind.JAXBException */ @SuppressWarnings("unchecked") public static <T> T unmarshal(Class<T> c, String xml) throws JAXBException { T res; if (c == xml.getClass()) { res = (T) xml; } else { JAXBContext ctx = JAXBContext.newInstance(c); Unmarshaller marshaller = ctx.createUnmarshaller(); res = (T) marshaller.unmarshal(new StringReader(xml)); } return res; }