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

StringasXml(T object)
as Xml
try {
    JAXBContext jaxbContext = null;
    jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    StringWriter writer = new StringWriter();
    marshaller.marshal(object, writer);
    return writer.toString();
} catch (JAXBException e) {
...
StringconvertObjectToXML(Object object)
convert Object To XML
StringWriter sw = new StringWriter();
String xmlString = null;
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
    jaxbMarshaller.marshal(object, sw);
    xmlString = sw.toString();
} catch (JAXBException e) {
...
StringconvertObjectToXml(Object object)
convert Object To Xml
return convertObjectToXml(object, "UTF-8");
StringconvertToString(Object obj)
convert To String
try {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    StringWriter writer = new StringWriter();
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(obj, writer);
    return writer.toString();
} catch (JAXBException e) {
...
StringconvertToXml(Object obj)
convert To Xml
return convertToXml(obj, "UTF-8");
StringconvertToXml(Object obj)
convert To Xml
return convertToXml(obj, "UTF-8");
StringconvertToXml(Object obj, String encoding)
convert To Xml
String result = null;
try {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
    StringWriter writer = new StringWriter();
    marshaller.marshal(obj, writer);
    result = writer.toString();
...
StringcreateXML(Object o)
create XML
JAXBContext jaxbContext = JAXBContext.newInstance(o.getClass());
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter result = new StringWriter();
jaxbMarshaller.marshal(o, result);
String xml = result.toString();
return xml;
ListgetObjects(List> objEls)
Get the list of T from list of JAXBElement .
List<T> list = new ArrayList<T>();
for (JAXBElement<? extends T> objEl : objEls) {
    list.add(objEl.getValue());
return list;
ListgetObjects(List objEls)
get Objects
final List list = new ArrayList();
for (final JAXBElement objEl : objEls) {
    list.add(objEl.getValue());
return list;