Example usage for javax.xml.bind JAXBElement JAXBElement

List of usage examples for javax.xml.bind JAXBElement JAXBElement

Introduction

In this page you can find the example usage for javax.xml.bind JAXBElement JAXBElement.

Prototype

public JAXBElement(QName name, Class<T> declaredType, T value) 

Source Link

Document

Construct an xml element instance.

Usage

From source file:Main.java

public static String object2Xml(Object obj) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(obj.getClass());
    Marshaller marshaller = context.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);

    StringWriter writer = new StringWriter();
    marshaller.marshal(new JAXBElement(new QName("xml"), obj.getClass(), obj), writer);

    return writer.toString();
}

From source file:Main.java

/**
 * Utility method to marshal a JAXB annotated java object to an XML
 * formatted string.  This class is generic enough to be used for any
 * JAXB annotated java object not containing the {@link XmlRootElement}
 * annotation.//www.j  a va  2s  .  c  o  m
 *
 * @param xmlClass   The JAXB class of the object to marshal.
 * @param xmlObject   The JAXB object to marshal.
 * @return      String containing the XML encoded object.
 */
public static String jaxbToString(Class<?> xmlClass, Object xmlObject) {

    // Make sure we are given the correct input.
    if (xmlClass == null || xmlObject == null) {
        return null;
    }

    @SuppressWarnings("unchecked")
    JAXBElement<?> jaxbElement = new JAXBElement(new QName("uri", "local"), xmlClass, xmlObject);

    return jaxbToString(xmlClass, jaxbElement);
}

From source file:Main.java

/**
 * Utility method that creates a new JAXBElement, which is used to marshal
 * the object/*from   w  w w .  java 2  s . co m*/
 * @param ns the name space
 * @param elementName the name element
 * @param clazz the class of the object to marshal
 * @param obj the object to marshal
 * @param <T> the type of the object
 * @return A JAXBElement that can be used to marshal the object
 */
public static <T> JAXBElement<T> newJAXBElement(String ns, String elementName, Class<T> clazz, T obj) {
    QName qName = new QName(ns, elementName);

    return new JAXBElement<T>(qName, clazz, obj);
}

From source file:Main.java

/**
 * Marshals a bean to XML./*w  w w  .ja v a2s  . c om*/
 * 
 * @param bean
 * @param namespaceURI
 * @param localPart
 * @return XML {@link String}
 * @throws JAXBException
 */
@SuppressWarnings("unchecked")
public static <T> String marshal(T bean, String namespaceURI, String localPart) throws JAXBException {
    QName qName = new QName(namespaceURI, localPart);
    Class<T> clazz = (Class<T>) bean.getClass();
    Marshaller marshaller = createMarshaller(clazz);
    Writer stw = new StringWriter();
    marshaller.marshal(new JAXBElement<T>(qName, clazz, bean), stw);

    return stw.toString();
}

From source file:controller.JaxbUtil.java

@SuppressWarnings("unchecked")
public String toXml(Collection root, String rootName, String encoding) {
    try {//  w w w .  java2  s  . co  m
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;
        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);
        StringWriter writer = new StringWriter();
        createMarshaller(encoding).marshal(wrapperElement, writer);
        return writer.toString();
    } catch (JAXBException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:com.common.util.mapper.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *//*from   w  w  w. j  ava 2 s . c  o  m*/
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    CollectionWrapper wrapper = new CollectionWrapper();
    wrapper.collection = root;

    JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
            CollectionWrapper.class, wrapper);

    StringWriter writer = new StringWriter();
    try {
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);
    } catch (JAXBException e) {
        e.printStackTrace();
    }

    return writer.toString();

}

From source file:com.plateform.common.util.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *///w ww  .j a  va 2s  .  c  om
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;

        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);

        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

        return writer.toString();
    } catch (JAXBException e) {
        throw ExceptionUtils.unchecked(e);
    }
}

From source file:com.androidwhy.modules.mapper.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *///from ww w . ja  v  a2s .  c o  m
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;

        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);

        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

        return writer.toString();
    } catch (JAXBException e) {
        throw Exceptions.unchecked(e);
    }
}

From source file:com.topsem.common.mapper.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *//*  w  ww.ja v a 2  s. c  o m*/
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;

        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);

        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

        return writer.toString();
    } catch (JAXBException e) {
        throw Throwables.propagate(e);
    }
}

From source file:com.wsun.seap.common.mapper.JaxbMapper.java

/**
 * Java Collection->Xml with encoding, ?Root ElementCollection.
 *//*from w  w  w  .ja v a 2  s  .co  m*/
public static String toXml(Collection<?> root, String rootName, Class clazz, String encoding) {
    try {
        CollectionWrapper wrapper = new CollectionWrapper();
        wrapper.collection = root;

        JAXBElement<CollectionWrapper> wrapperElement = new JAXBElement<CollectionWrapper>(new QName(rootName),
                CollectionWrapper.class, wrapper);

        StringWriter writer = new StringWriter();
        createMarshaller(clazz, encoding).marshal(wrapperElement, writer);

        return writer.toString();
    } catch (JAXBException e) {
        throw ExceptionUtil.unchecked(e);
    }
}