List of usage examples for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT
String JAXB_FORMATTED_OUTPUT
To view the source code for javax.xml.bind Marshaller JAXB_FORMATTED_OUTPUT.
Click Source Link
From source file:Main.java
public static <T> void saveObject(T object, Class<T> typeClass, String path) { try {// w ww . j ava 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
/** * //from w ww . j a v a 2 s . co m * @param object * @return * @throws JAXBException */ public static String pojoToXml(Object object) throws JAXBException { JAXBContext context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); // marshaller.setProperty(Marshaller.JAXB_ENCODING, "U"); StringWriter writer = new StringWriter(); marshaller.marshal(object, writer); String xmlData = writer.toString(); return xmlData; }
From source file:Main.java
/** * Saves the object to the file./* ww w .j ava 2 s .co m*/ * * @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
/** * Parse XML using JAXB and a model class. * /*from w ww . j ava2 s . co 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
public static <T> void serialize(T object, OutputStream out) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(object, out); }
From source file:Main.java
public static String bean2Xml(Object bean, String codetype) { String xmlString = null;/*from w w w. j ava2 s .c o m*/ JAXBContext context; StringWriter writer; if (null == bean) return xmlString; try { context = JAXBContext.newInstance(bean.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, false);// // m.setProperty(Marshaller.JAXB_ENCODING, "gb2312");// // m.setProperty(Marshaller.JAXB_ENCODING, "GBK");// m.setProperty(Marshaller.JAXB_ENCODING, codetype);// m.setProperty(Marshaller.JAXB_FRAGMENT, false);// writer = new StringWriter(); m.marshal(bean, writer); xmlString = writer.toString(); return xmlString; } catch (Exception e) { e.printStackTrace(); } return xmlString; }
From source file:Main.java
public static void serialize(Object o, OutputStream os, Boolean format) throws JAXBException { Marshaller m = CTX.createMarshaller(); m.setEventHandler(new DefaultValidationEventHandler()); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format); m.marshal(o, os);/*w ww.j a va 2s.c o m*/ }
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> Document marshal(Object jaxbElement, Class<T> jaxbFactory) throws JAXBException { if (!(jaxbElement instanceof JAXBElement<?>)) { throw new JAXBException("Must be a instance of JAXBElement<?>"); }/*from w ww .j a va 2 s .c o m*/ 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:com.technofovea.hl2parse.vdf.MaterialReader.java
public static final MaterialRefList loadFromXml(InputStream stream) throws JAXBException { JAXBContext jc = JAXBContext.newInstance(MaterialReference.class, MaterialRefList.class); Unmarshaller um = jc.createUnmarshaller(); Marshaller m = jc.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); Object o = um.unmarshal(stream); return (MaterialRefList) o; }