Java Utililty Methods XML JAXB Marshaller

List of utility methods to do XML JAXB Marshaller

Description

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

Method

Stringmarshall(Object o)
marshall
return marshall(o, o.getClass());
Stringmarshall(Object obj)
marshall
JAXBContext context = JAXBContext.newInstance(obj.getClass());
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter w = new StringWriter();
m.marshal(obj, w);
return w.toString();
Stringmarshall(Object obj, URL schemaURL, Class... classesToBeBound)
marshall
JAXBContext context = JAXBContext.newInstance(classesToBeBound);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
if (schemaURL != null) {
    SchemaFactory sf = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
    Schema schema = sf.newSchema(schemaURL);
    marshaller.setSchema(schema);
StringWriter writer = new StringWriter();
marshaller.marshal(obj, writer);
return writer.toString();
InputStreammarshall(Object toMarshall)
Marshalls the object to a String.
StringWriter sw = null;
try {
    JAXBContext ctx = JAXBContext.newInstance(toMarshall.getClass().getPackage().getName());
    sw = new StringWriter();
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty("jaxb.formatted.output", true);
    marshaller.marshal(toMarshall, sw);
} catch (Exception e) {
...
voidmarshall(OutputStream os, JAXBElement element)
marshall
JAXBContext context = JAXBContext.newInstance(element.getDeclaredType());
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(element, os);
voidmarshall(String cntxtPkg, Object obj, OutputStream out)
marshall
Marshaller marshaller = createMarshall(cntxtPkg);
if (marshaller == null)
    return;
marshaller.marshal(obj, out);
voidmarshall(String file, JAXBElement object, Class context)
marshall
JAXBContext ctx = JAXBContext.newInstance(context);
Marshaller marshaller = ctx.createMarshaller();
marshaller.marshal(object, new File(file));
Stringmarshaller(Object o, Class T)
marshaller
JAXBContext jc;
Marshaller marshaller;
StringWriter writer = new StringWriter();
try {
    jc = JAXBContext.newInstance(T);
    marshaller = jc.createMarshaller();
    marshaller.marshal(o, writer);
} catch (JAXBException e) {
...
voidmarshaller(Object obj, File file)
marshaller
JAXBContext jc = JAXBContext.newInstance(obj.getClass());
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(obj, file);
StringmarshallerObject(Class c, Object o)
marshaller Object
String s = "<?xml version=\"1.0\" ?>";
try {
    JAXBContext jaxbContext = JAXBContext.newInstance(c);
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_ENCODING, "utf-8");
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
    StringWriter baos = new StringWriter();
...