List of usage examples for javax.xml.bind Marshaller setProperty
public void setProperty(String name, Object value) throws PropertyException;
From source file:Main.java
public final static void save(Object object, String path) { try {//from ww w . ja v a 2 s .c o m File file = new File(path); JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, file); } catch (JAXBException e) { e.printStackTrace(); } }
From source file:Main.java
/** * Saves the data in the file in xml format. * * @param file Points to a valid xml file containing data that match the {@code classToConvert}. * Cannot be null./*from w ww . ja v a2s .c o m*/ * @throws FileNotFoundException Thrown if the file is missing. * @throws JAXBException Thrown if there is an error during converting the data * into xml and writing to the file. */ public static <T> void saveDataToFile(File file, T data) throws FileNotFoundException, JAXBException { assert file != null; assert data != null; if (!file.exists()) { throw new FileNotFoundException("File not found : " + file.getAbsolutePath()); } JAXBContext context = JAXBContext.newInstance(data.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(data, file); }
From source file:Main.java
/** * Parse XML using JAXB and a model class. * //w w w .j ava 2 s .c o m * @param in an input stream * @return the requested object * @param cls the root class * @throws JAXBException thrown on an error */ @SuppressWarnings("unchecked") public static <T> T parseJaxb(Class<T> cls, InputStream in) throws JAXBException { JAXBContext context = JAXBContext.newInstance(cls); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Unmarshaller um = context.createUnmarshaller(); return (T) um.unmarshal(in); }
From source file:Main.java
private static Marshaller createMarshaller(Object o) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); return jaxbMarshaller; }
From source file:Main.java
/** * Prints the document for debug.//from w ww .ja v a2s. c o m * * @param model the model to be printed * @throws Exception for any errors encountered */ public static void printModel(Object model) throws Exception { JAXBContext jaxbContext = JAXBContext.newInstance("gov.medicaid.domain.model"); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(model, System.out); }
From source file:Main.java
public static String getXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema) throws JAXBException { String packageName = versioningInfo.getValue().getClass().getPackage().getName(); JAXBContext context = JAXBContext.newInstance(packageName); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, formatXml); if (schema != null) { marshaller.setSchema(schema);/*ww w. ja v a 2s. c o m*/ } ByteArrayOutputStream oStream = new ByteArrayOutputStream(); marshaller.marshal(versioningInfo, oStream); return oStream.toString(); }
From source file:Main.java
public static String toXML(Object obj) { try {//from ww w . j ava2s . c o m JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); return writer.toString(); } catch (JAXBException e) { e.printStackTrace(); return ""; } }
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
/** * Helper method to serialize an object to XML. Requires object to be Serializable * @param entity Object to serialize// w ww . ja v a 2 s .co m * @param <T> class that implements Serializable * @return String XML representation of object * @throws IOException if errors during serialization * @throws JAXBException if errors during serialization */ public static <T extends Serializable> String getXml(T entity) throws IOException, JAXBException { StringWriter sw = new StringWriter(); if (null != entity) { JAXBContext ctx = JAXBContext.newInstance(entity.getClass()); Marshaller m = ctx.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(entity, sw); } sw.flush(); sw.close(); return sw.toString(); }
From source file:Main.java
/** * //from w ww.j ava2s . 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; }