Java Utililty Methods XML JAXB Object to XML

List of utility methods to do XML JAXB Object to XML

Description

The list of methods to do XML JAXB Object to XML are organized into topic(s).

Method

byte[]toXML(final Object obj, final boolean prettyPrint)
Converts object to XML and returns the byte array of the XML text created.
return toXML(obj, prettyPrint, null);
StringtoXml(final T dto)
to Xml
final CharArrayWriter caw = new CharArrayWriter();
toXml(dto, caw);
return caw.toString();
StringtoXML(Object o)
to XML
StringWriter writer = new StringWriter();
javax.xml.bind.JAXB.marshal(o, writer);
return writer.toString();
voidtoXML(Object o, OutputStream os)
to XML
JAXBContext context = JAXBContext.newInstance(o.getClass());
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(o, os);
voidtoXml(Object o, OutputStream output)
to Xml
try {
    JAXBContext context = JAXBContext.newInstance(o.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, CHARSET);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(o, new OutputStreamWriter(output, CHARSET));
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
...
StringtoXml(Object obj)
to Xml
StringWriter sw = new StringWriter();
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(obj, sw);
return sw.toString();
StringtoXml(Object object)
to Xml
final StringWriter out = new StringWriter();
JAXBContext context = null;
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) {
...
StringtoXML(Object thing)
to XML
StringWriter sw = new StringWriter();
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(thing.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.marshal(thing, sw);
    return sw.toString();
...
StringtoXml(T element)
to Xml
try {
    Marshaller marshaller = JAXBContext.newInstance(element.getClass()).createMarshaller();
    StringWriter writer = new StringWriter();
    marshaller.marshal(element, writer);
    return writer.toString();
} catch (JAXBException e) {
    throw new IllegalStateException(e);
StringtoXml(T object)
to Xml
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter writer = new StringWriter();
marshaller.marshal(object, writer);
return writer.toString();