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 toXml(Class className, Object object) { String strXml = ""; StringWriter writer = null;//from w w w . jav a 2 s . com try { writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(className); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, writer); strXml = writer.toString(); writer.flush(); strXml = strXml.replace("<", "<"); strXml = strXml.replace(">", ">"); } catch (Exception e) { } finally { if (writer != null) { try { writer.close(); writer = null; } catch (Exception e) { } } } return strXml; }
From source file:Main.java
public static String obj2Xml(Object obj, String encoding) { String result = null;/*from w w w . j ava 2 s .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); StringWriter writer = new StringWriter(); marshaller.marshal(obj, writer); result = writer.toString(); } catch (Exception e) { e.printStackTrace(); } return result; }
From source file:Main.java
public static String marshallToStringNoValidation(final Object xmlObject) throws JAXBException { StringWriter writer = new StringWriter(); JAXBContext context = JAXBContext.newInstance(xmlObject.getClass()); Marshaller jaxbMarshaller = context.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(xmlObject, writer); return writer.toString(); }
From source file:Main.java
public static String convertObjectToXML(Object object) { JAXBContext jaxbContext = null; Marshaller jaxbMarshaller = null; StringWriter stringWriter = new StringWriter(); try {//w w w .j a v a 2s .c om jaxbContext = JAXBContext.newInstance(object.getClass()); jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); jaxbMarshaller.marshal(object, stringWriter); } catch (JAXBException e) { e.printStackTrace(); } String xmlString = stringWriter.toString(); return xmlString; }
From source file:Main.java
public static void saveInstance(OutputStream outputStream, Object instance) throws JAXBException, IOException { Marshaller marshaller = JAXBContext.newInstance(instance.getClass()).createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.setProperty(Marshaller.JAXB_ENCODING, ENCODING); marshaller.marshal(instance, outputStream); outputStream.flush();//from www .j a va 2 s. c o m }
From source file:Main.java
public static void marshal(Class<?> klass, Object obj, OutputStream os) throws JAXBException { try {/*from w w w . ja va 2s . c om*/ 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."); } try { marshaller.marshal(obj, os); } catch (JAXBException jaxbe) { jaxbe.printStackTrace(); } }
From source file:Main.java
public static String toXml(Object object) { final StringWriter out = new StringWriter(); JAXBContext context = null;/*ww w . j a v a 2 s .c om*/ try { context = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(javax.xml.bind.Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); marshaller.marshal(object, new StreamResult(out)); } catch (PropertyException e) { e.printStackTrace(); } catch (JAXBException e) { e.printStackTrace(); } return out.toString(); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> String marshal(T object) throws IOException, JAXBException { Class<T> clzz = (Class<T>) object.getClass(); JAXBContext context;/*from w w w . ja v a 2 s . c om*/ context = JAXBContext.newInstance(clzz); Marshaller m = context.createMarshaller(); ByteArrayOutputStream os = new ByteArrayOutputStream(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); m.marshal(object, os); return os.toString(); }
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);/*from www .j a va2 s .com*/ } ByteArrayOutputStream oStream = new ByteArrayOutputStream(); marshaller.marshal(versioningInfo, oStream); return oStream.toString(); }
From source file:Main.java
public static <T> void saveObject(T object, Class<T> typeClass, URL path) { try {/*w ww . ja v a 2 s .c o m*/ 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(); } }