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

StringtoXMLString(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);
...
StringtoXMLString(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) {
...
StringtoXmlString(T obj, Class type)
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);
voidwrite(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);
Filewrite(String filepath, T content, Class typeParameterClass)
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) {
...
voidwriteJaxbObject(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);
StringxmlEntityToString(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) {
...
StringXmlObjectToString(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();
...