List of utility methods to do XML JAXB Object to XML
String | toXMLString(final T binding) to XML String final StringWriter stringWriter = new StringWriter(); final JAXBContext jaxbContext = JAXBContext.newInstance(binding.getClass()); final Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); final XmlRootElement xmlRootElement = binding.getClass().getAnnotation(XmlRootElement.class); if (xmlRootElement != null) { marshaller.marshal(binding, stringWriter); ... |
String | toXMLString(Object obj) Convert an object with JAXB annotations to an XML string try { JAXBContext context = JAXBContext.newInstance(obj.getClass()); Marshaller m = context.createMarshaller(); m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); StringWriter writer = new StringWriter(); m.marshal(obj, writer); return writer.toString(); } catch (Throwable e) { ... |
String | toXmlString(T obj, Class to Xml String try { JAXBContext jaxbContext = JAXBContext.newInstance(type); ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); jaxbContext.createMarshaller().marshal(obj, byteArrayOutputStream); return byteArrayOutputStream.toString(); } catch (JAXBException e) { throw new RuntimeException(e); |
void | write(Object jaxbAnnotatedObj, OutputStream os) write JAXBContext context = JAXBContext.newInstance(jaxbAnnotatedObj.getClass()); Marshaller marshaller = context.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(jaxbAnnotatedObj, os); |
File | write(String filepath, T content, Class Creates a new XML file and writes the content of the specified object into the XML file. File file = null; try { file = new File(filepath); if (!file.exists()) { file.createNewFile(); file = write(file, content, typeParameterClass); } catch (IOException e) { ... |
void | writeJaxbObject(OutputStream outputStream, JAXBElement> jaxbElement, Class> modelClass) write Jaxb Object JAXBContext context = JAXBContext.newInstance(modelClass.getPackage().getName());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(jaxbElement, outputStream);
|
String | xmlEntityToString(final T entity) xml Entity To String try { JAXBContext jaxbContext = JAXBContext.newInstance(entity.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ByteArrayOutputStream out = new ByteArrayOutputStream(); marshaller.marshal(entity, out); return out.toString(); } catch (final JAXBException e) { ... |
String | XmlObjectToString(Object o) Xml Object To String OutputStream baos = new ByteArrayOutputStream(); try { JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass()); Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); jaxbMarshaller.marshal(o, baos); } catch (JAXBException e) { e.printStackTrace(); ... |