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 String marshalToString(Class<?> klass, Object obj) throws JAXBException { try {//from w w w . j a v a2 s .c o m context = JAXBContext.newInstance(klass); marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); unmarshaller = context.createUnmarshaller(); } catch (JAXBException e) { throw new RuntimeException( "There was a problem creating a JAXBContext object for formatting the object to XML."); } StringWriter sw = new StringWriter(); String result = null; try { marshaller.marshal(obj, sw); result = sw == null ? null : sw.toString(); } catch (JAXBException jaxbe) { jaxbe.printStackTrace(); } return result; }
From source file:Main.java
/** * genXml//www .java 2 s.co m * * @param o Object Class. * @param path : example c:\path\006DS_AMS20130630.xml */ public static void genXml(Object o, String path) { try { // create JAXB context and initializing Marshaller JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); // for getting nice formatted output jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); //specify the location and name of xml file to be created File XMLfile = new File(path); // Writing to XML file jaxbMarshaller.marshal(o, XMLfile); // Writing to console jaxbMarshaller.marshal(o, System.out); } catch (JAXBException e) { // some exception occured e.printStackTrace(); } }
From source file:Main.java
/** * @since 2.4//from w ww . jav a2s . c om */ public static Marshaller createMarshaller(final Object object) throws JAXBException { JAXBContext context = createContext(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); return marshaller; }
From source file:Main.java
/** * Object to XML// w w w .j a va2s . c o m * @param object * @return */ public static String convertToXML(Object object) { try { if (!mMap.containsKey(object.getClass())) { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); mMap.put(object.getClass(), marshaller); } 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
public static void Object2XmlFile(Object ob, String path) throws JAXBException, FileNotFoundException { Class local = ob.getClass();//from ww w .j av a 2 s. com JAXBContext context = JAXBContext.newInstance(new Class[] { local }); Marshaller marshaller = context.createMarshaller(); // marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", // paramNamespacePrefixMapper); // marshaller.setProperty("jaxb.formatted.output", // Boolean.valueOf(true)); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(ob, new FileOutputStream(new File(path))); }
From source file:Main.java
/** * this method is responsible for converting any pojo to respective xml form * //from w w w .jav a2 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
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
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 w ww .j av a2s . c om*/ return string.toString(); }
From source file:Main.java
/** * Object to XML//from w w w. ja 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
public static String objectToXml(JAXBContext jaxbContext, Object object) throws JAXBException { StringWriter writerTo = new StringWriter(); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writerTo); return writerTo.toString(); }