Java XML JAXB Object to XML toXMLString(final T binding)

Here you can find the source of toXMLString(final T binding)

Description

to XML String

License

Open Source License

Declaration

@SuppressWarnings("unchecked")
    public static <T> String toXMLString(final T binding) throws JAXBException 

Method Source Code

//package com.java2s;
/* Copyright (c) 2017 lib4j
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * You should have received a copy of The MIT License (MIT) along with this
 * program. If not, see <http://opensource.org/licenses/MIT/>.
 *//*from  w w  w .  ja v  a  2 s.c om*/

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchema;
import javax.xml.bind.annotation.XmlType;
import javax.xml.namespace.QName;

public class Main {
    private static final String DEFAULT = "##default";

    @SuppressWarnings("unchecked")
    public static <T> String toXMLString(final T binding) throws JAXBException {
        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);
            return stringWriter.toString();
        }

        final XmlType xmlType = binding.getClass().getAnnotation(XmlType.class);
        final String localName = DEFAULT.equals(xmlType.name()) ? binding.getClass().getSimpleName()
                : xmlType.name();
        final String namespace;
        if (DEFAULT.equals(xmlType.namespace())) {
            final XmlSchema xmlSchema = binding.getClass().getPackage().getAnnotation(XmlSchema.class);
            namespace = xmlSchema != null ? xmlSchema.namespace() : DEFAULT;
        } else {
            namespace = xmlType.namespace();
        }

        final QName qName = new QName(namespace, localName);
        final JAXBElement<T> element = new JAXBElement<>(qName, (Class<T>) binding.getClass(), binding);
        marshaller.marshal(element, stringWriter);
        return stringWriter.toString();
    }
}

Related

  1. toXml(Object obj)
  2. toXml(Object object)
  3. toXML(Object thing)
  4. toXml(T element)
  5. toXml(T object)
  6. toXMLString(Object obj)
  7. toXmlString(T obj, Class type)
  8. write(Object jaxbAnnotatedObj, OutputStream os)
  9. write(String filepath, T content, Class typeParameterClass)