List of usage examples for javax.xml.bind Marshaller marshal
public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;
From source file:Main.java
public static String toXxml(Object bean) { StringWriter stringWriter = null; try {/* www .j ava2 s . c o m*/ JAXBContext jaxbContext = JAXBContext.newInstance(bean.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); stringWriter = new StringWriter(); marshaller.marshal(bean, stringWriter); String result = stringWriter.toString(); // remove xml declaration result = result.replaceFirst(".*\n", ""); return result; } catch (JAXBException e) { throw new RuntimeException(e); } finally { if (stringWriter != null) try { stringWriter.close(); } catch (IOException e) { e.printStackTrace(); } } }
From source file:eu.hydrologis.jgrass.featureeditor.utils.Utilities.java
/** * Write the {@link AForm} to xml file./*from w ww .j a v a 2 s. c o m*/ * * @param form the {@link AForm} object. * @param file the file to which to dump to. * @throws Exception */ public static void writeXML(AForm form, File file) throws Exception { JAXBContext jc = JAXBContext.newInstance(AForm.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(form, file); }
From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java
/** * Marshals an object into a formatted XML document * @param element the JAXB element object that represents an 'object' of * type T// ww w . j a v a 2s . c om * @param object the object to be transformed into XML * @param writer the writer object * @throws JAXBException if unable to transform the object to XML */ public static <T> void marshal(JAXBElement<T> element, T object, Writer writer) throws JAXBException { JAXBContext context = JAXBContext.newInstance(object.getClass().getPackage().getName()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(element, writer); }
From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java
/** * Marshals an object into a formatted XML document * @param element the JAXB element object that represents an 'object' of * type T/*from ww w . j a va2 s . co m*/ * @param object the object to be transformed into XML * @param stream the output stream * @throws JAXBException if unable to transform the object to XML */ public static <T> void marshal(JAXBElement<T> element, T object, OutputStream stream) throws JAXBException { JAXBContext context = JAXBContext.newInstance(object.getClass().getPackage().getName()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); m.marshal(element, stream); }
From source file:Main.java
public static <T> void saveObject(T object, Class<T> typeClass, String path) { try {//from w w w . j av a 2 s .co m 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 void marshal(Object model, OutputStream output) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(model.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); jaxbMarshaller.marshal(model, output); }
From source file:Main.java
/** * this method is responsible for converting any pojo to respective xml form * /* w ww . ja va 2 s . c o m*/ * @param object * @param filePath * @throws JAXBException * @throws IOException */ public static File pojoToXml(Object object, String filePath) throws JAXBException, IOException { JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); OutputStream os = new FileOutputStream(filePath); marshaller.marshal(object, os); File file = new File(filePath); return file; }
From source file:Main.java
/** * Method to write an XML object in the given filePath. * //from ww w .ja v a 2 s . co m * @param <S> * The type of object that needs to written in XML file. * @param filePath * Path of file where xml object gets written * @param object * The Object that needs to be written in XML file. * @throws Exception */ public static <S> void writeXMLObject(String filePath, S object) throws Exception { // java XML context object. JAXBContext jc = JAXBContext.newInstance(object.getClass()); // file. File file = new File(filePath); // Creating marshaller Marshaller marshaller = jc.createMarshaller(); // Setting output format marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // Marshalling object. marshaller.marshal(object, file); }
From source file:Main.java
public static <T> void saveObject(T object, Class<T> typeClass, URL path) { try {/*from w ww. ja v a2 s . com*/ 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
/** * * @param obj//from ww w.ja v a2 s. c o m * @param os * @throws JAXBException */ public static void serialize(Object obj, OutputStream os) throws JAXBException { final Marshaller m = JAXBContext.newInstance(obj.getClass()).createMarshaller(); m.setProperty(JAXB_FRAGMENT, TRUE); m.marshal(obj, os); }