List of usage examples for javax.xml.bind JAXBContext createMarshaller
public abstract Marshaller createMarshaller() throws JAXBException;
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<?>"); }/* www .j a v a2 s .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
/** * Saves the object to the file.//from w ww. j a va 2 s . c om * * @param <T> the object type. * @param path the XML file. * @param object the object to be saved. */ public static <T> void saveObject(Path path, T object) { if (path == null || object == null) { throw new RuntimeException("The path to file or object is null!"); } try { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, path.toFile()); } catch (Exception ex) { throw new RuntimeException("Error saving the object to path " + path.toString(), ex); } }
From source file:Main.java
public static <T> void serialize(T object, OutputStream resultStream) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, resultStream); }
From source file:Main.java
public static <T> void serialize(T object, OutputStream resultStream, String schemaLocation) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, resultStream); }
From source file:Main.java
public static <T> String asXml(T object) { try {//from w ww. j a va2s . c om JAXBContext jaxbContext = null; jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshal(object, writer); return writer.toString(); } 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 .j a v 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> Document marshal(Object jaxbElement, Class<T> jaxbFactory) throws JAXBException { if (!(jaxbElement instanceof JAXBElement<?>)) { throw new JAXBException("Must be a instance of JAXBElement<?>"); }// w w w. j a v a 2 s .c om Document doc = null; try { JAXBContext jc = JAXBContext.newInstance(jaxbFactory); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); doc = builder.newDocument(); marshaller.marshal(jaxbElement, doc); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } return doc; }
From source file:Main.java
public static <T> Element marshal(JAXBElement<T> jaxbElement, Class<T> cls) { try {//from www . ja va 2 s . c o m DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.newDocument(); JAXBContext jaxbContext = JAXBContext.newInstance(cls); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.marshal(jaxbElement, doc); return doc.getDocumentElement(); } catch (JAXBException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ParserConfigurationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }
From source file:Main.java
public static String convertToXml(Object obj, String encoding) { String result = null;//ww w .j ava 2s .c o m try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); // marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding); // marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, true); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:net.firejack.aws.license.LicenseHelper.java
public static File create(License license) throws IOException, NoSuchAlgorithmException, JAXBException { signature(license);/*from w w w .j a va2s. c o m*/ File tmp = new File("/tmp/", license.getName() + ".xml"); tmp.getParentFile().mkdirs(); tmp.createNewFile(); FileOutputStream stream = new FileOutputStream(tmp); JAXBContext jaxbContext = JAXBContext.newInstance(License.class); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(license, stream); stream.close(); return tmp; }