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

StringgetXmlString(JAXBElement versioningInfo, Boolean formatXml, Schema schema)
get Xml String
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);
ByteArrayOutputStream oStream = new ByteArrayOutputStream();
...
StringgetXMLString(Object person, Class clazz)
get XML String
StringWriter sw = new StringWriter();
JAXBContext context = JAXBContext.newInstance(clazz);
context.createMarshaller().marshal(person, sw);
String output = sw.toString();
System.out.println(output);
return output;
StringgetXmlString(T jaxbObject)
get Xml String
StringWriter sw = new StringWriter();
JAXBContext context = JAXBContext.newInstance(jaxbObject.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(jaxbObject, sw);
return sw.toString();
StringObject2Xml(Object object)
Object Xml
Marshaller mar = createMarshallerByClazz(object.getClass());
StringWriter strWriter = new StringWriter();
mar.marshal(object, strWriter);
return strWriter.toString();
StringobjectToXML(Class cls, Object entity)
Return the xml translation of an object
try {
    Marshaller m = JAXBContext.newInstance(cls).createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
    StringWriter sw = new StringWriter();
    m.marshal(entity, sw);
    return sw.toString();
} catch (Exception e) {
...
StringObjectToXml(Object object)
Transforms an annotated Object to a XML string using javax.xml.bind.Marshaller
JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass());
Marshaller marshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
marshaller.marshal(object, sw);
return sw.toString();
StringobjectToXml(Object source, Class... type)
object To Xml
String result;
StringWriter sw = new StringWriter();
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(type);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(source, sw);
    result = sw.toString();
...
StringtoString(E e)
to String
String charsetName = "utf-8";
ByteArrayOutputStream os = new ByteArrayOutputStream();
print(e, os, e.getClass());
try {
    return os.toString(charsetName);
} catch (UnsupportedEncodingException e1) {
    throw new IllegalArgumentException(charsetName + " is not supported");
StringtoString(Object o, Class clazz)
toString marshals given Jaxb object to a string (useful for debug)
StringWriter sw = new StringWriter();
try {
    JAXBContext jc = JAXBContext.newInstance(clazz);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(o, sw);
} catch (Exception e) {
    e.printStackTrace();
...
StringtoXml(Class className, Object object)
to Xml
String strXml = "";
StringWriter writer = null;
try {
    writer = new StringWriter();
    JAXBContext context = JAXBContext.newInstance(className);
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.marshal(object, writer);
...