Example usage for javax.xml.bind JAXBContext createMarshaller

List of usage examples for javax.xml.bind JAXBContext createMarshaller

Introduction

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

Prototype

public abstract Marshaller createMarshaller() throws JAXBException;

Source Link

Document

Create a Marshaller object that can be used to convert a java content tree into XML data.

Usage

From source file:com.biomeris.i2b2.export.ws.messages.MessageBuilder.java

public static String buildPMGetServiceRequest(Network network) throws JAXBException {
    com.biomeris.i2b2.export.datavo.pm.ObjectFactory pmObjectFactory = new com.biomeris.i2b2.export.datavo.pm.ObjectFactory();
    com.biomeris.i2b2.export.ws.messages.extensions.ObjectFactory extObjectFactory = new com.biomeris.i2b2.export.ws.messages.extensions.ObjectFactory();

    ProxyedRequestMessageType proxyedRequestMessageType = new ProxyedRequestMessageType();

    Proxy proxy = new Proxy();
    proxy.setRedirectUrl(network.getPmServiceAddress() + PMSERVICE_NAME);
    proxyedRequestMessageType.setProxy(proxy);

    MessageHeaderType messageHeaderType = new MessageHeaderType();
    SecurityType securityType = new SecurityType();
    securityType.setDomain(network.getDomain());
    securityType.setUsername(network.getUsername());
    PasswordType passwordType = JAXB.unmarshal(new StringReader(network.getPassword()), PasswordType.class);
    securityType.setPassword(passwordType);
    messageHeaderType.setSecurity(securityType);
    messageHeaderType.setProjectId(network.getProject());
    ApplicationType applicationType = new ApplicationType();
    applicationType.setApplicationName("Export Cell");
    applicationType.setApplicationVersion("1.0");
    messageHeaderType.setSendingApplication(applicationType);
    proxyedRequestMessageType.setMessageHeader(messageHeaderType);

    RequestHeaderType requestHeaderType = new RequestHeaderType();
    requestHeaderType.setResultWaittimeMs(1800000);
    proxyedRequestMessageType.setRequestHeader(requestHeaderType);

    BodyType bodyType = new BodyType();

    GetUserConfigurationType g = pmObjectFactory.createGetUserConfigurationType();
    g.getProject().add("undefined");
    JAXBElement<GetUserConfigurationType> any1 = pmObjectFactory.createGetUserConfiguration(g);

    bodyType.getAny().add(any1);/*from w  w w  .  j ava 2 s. co  m*/
    proxyedRequestMessageType.setMessageBody(bodyType);

    JAXBContext jc = JAXBContext.newInstance(ProxyedRequestMessageType.class);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter sw = new StringWriter();
    JAXBElement<ProxyedRequestMessageType> xxx = extObjectFactory.createRequestPM(proxyedRequestMessageType);
    m.marshal(xxx, sw);

    return sw.toString();
}

From source file:gov.hhs.fha.nhinc.lift.proxy.util.ProxyUtil.java

public static org.w3c.dom.Element marshal(Object obj) throws JAXBException {
    try {//from  w w w .j  a va  2  s .c o  m
        Document doc = null;
        JAXBContext jc = JAXBContext.newInstance(obj.getClass());
        Marshaller marshaller = jc.createMarshaller();
        javax.xml.parsers.DocumentBuilderFactory dbf;
        dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();
        dbf.setNamespaceAware(true);
        doc = dbf.newDocumentBuilder().newDocument();
        marshaller.marshal(obj, doc);
        log.info("Marshal: " + doc.getNodeValue());
        return doc.getDocumentElement();
    } catch (ParserConfigurationException ex) {
        throw new JAXBException("Unable to create document: " + ex.getMessage());
    }
}

From source file:Main.java

public static String objectToXml(Object object) {
    if (null == object) {
        return NO_RESULT;
    }//from ww w  .ja  v a 2s  .c om

    StringWriter sw = new StringWriter();
    JAXBContext jAXBContent = null;
    Marshaller marshaller = null;

    try {
        jAXBContent = JAXBContext.newInstance(object.getClass());
        marshaller = jAXBContent.createMarshaller();
        marshaller.marshal(object, sw);

        return sw.toString();
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    return NO_RESULT;
}

From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java

/**
 * Marshals an object into a formatted XML document
 * @param element the JAXB element object that represents an 'object' of
 * type T/*from   w  ww  .  j  ava2 s  .c om*/
 * @param object the object to be transformed into XML
 * @param writer the writer object
 * @throws JAXBException if unable to transform the object to XML
 */
public static <T> void marshal(JAXBElement<T> element, T object, Writer writer) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(object.getClass().getPackage().getName());

    Marshaller m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(element, writer);
}

From source file:net.orpiske.sas.commons.xml.XmlWriterUtils.java

/**
 * Marshals an object into a formatted XML document
 * @param element the JAXB element object that represents an 'object' of
 * type T/*  w w  w  .  j ava2 s.  c  o m*/
 * @param object the object to be transformed into XML
 * @param stream the output stream
 * @throws JAXBException if unable to transform the object to XML
 */
public static <T> void marshal(JAXBElement<T> element, T object, OutputStream stream) throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(object.getClass().getPackage().getName());

    Marshaller m = context.createMarshaller();

    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.marshal(element, stream);
}

From source file:eu.planets_project.tb.impl.serialization.JaxbUtil.java

/**
* Marshalling via Jaxb: Creates a String Serialization of the requested class for
* the content of Object objectToSerialize. The provided object and the requested class need to be of the same Type
* @param <T>//from   w  ww .  ja  v  a2  s  . co m
* @param objectClass
* @param objectToSerialize
* @return
* @throws Exception
*/
public static <T> String marshallObjectwithJAXB(Class<T> objectClass, T objectToSerialize) throws Exception {
    JAXBContext context;
    try {
        context = JAXBContext.newInstance(objectClass);
        Marshaller m = context.createMarshaller();
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        StringWriter sw = new StringWriter();
        //now call the actual marshalling job
        m.marshal(objectToSerialize, sw);
        return sw.toString();
    } catch (Exception e) {
        log.error("marshalWorkflowResult failed for objectClass: " + objectClass + " with " + e);
        throw e;
    }
}

From source file:Main.java

/**
 * Method to write an XML object in the given filePath.
 * //from   w w w .  j av a  2  s  . c  o  m
 * @param <S>
 *            The type of object that needs to written in XML file.
 * @param filePath
 *            Path of file where xml object gets written
 * @param object
 *            The Object that needs to be written in XML file.
 * @throws Exception
 */
public static <S> void writeXMLObject(String filePath, S object) throws Exception {
    // java XML context object.
    JAXBContext jc = JAXBContext.newInstance(object.getClass());
    // file.
    File file = new File(filePath);
    // Creating marshaller
    Marshaller marshaller = jc.createMarshaller();
    // Setting output format
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    // Marshalling object.
    marshaller.marshal(object, file);
}

From source file:Main.java

public static String jaxbToString(Class<?> xmlClass, JAXBElement<?> jaxbElement) {

    // Make sure we are given the correct input.
    if (xmlClass == null || jaxbElement == null) {
        return null;
    }//  ww w.  j  a  v  a  2s.  c  o m

    // We will write the XML encoding into a string.
    StringWriter writer = new StringWriter();
    String result;
    try {
        // We will use JAXB to marshal the java objects.
        final JAXBContext jaxbContext = JAXBContext.newInstance(xmlClass);

        // Marshal the object.
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(jaxbElement, writer);
        result = writer.toString();
    } catch (Exception e) {
        // Something went wrong so get out of here.
        return null;
    } finally {
        try {
            writer.close();
        } catch (IOException ex) {
        }
    }

    // Return the XML string.
    return result;
}

From source file:de.iteratec.iteraplan.businesslogic.service.SavedQueryXmlHelper.java

private static Marshaller getMarshaller(Class<?> clazz, String schemaName) throws JAXBException, SAXException {
    JAXBContext context = JAXBContext.newInstance(clazz);
    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    m.setSchema(getSchema(schemaName));/*from  w w w. j a  va2  s .c om*/
    m.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, SCHEMA_URI + " " + SCHEMA_URI + schemaName);

    return m;
}

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

/**
 * Marshallerencoding(?null).//from   w  w  w. ja  v  a2  s . c o m
 * ???pooling
 */
public static Marshaller createMarshaller(Class clazz, String encoding) throws JAXBException {
    JAXBContext jaxbContext = getJaxbContext(clazz);

    Marshaller marshaller = jaxbContext.createMarshaller();

    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

    if (StringUtils.isNotBlank(encoding)) {
        marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
    }

    return marshaller;

}