Example usage for javax.xml.bind Marshaller marshal

List of usage examples for javax.xml.bind Marshaller marshal

Introduction

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

Prototype

public void marshal(Object jaxbElement, javax.xml.stream.XMLEventWriter writer) throws JAXBException;

Source Link

Document

Marshal the content tree rooted at jaxbElement into a javax.xml.stream.XMLEventWriter .

Usage

From source file:org.cbio.portal.pipelines.foundation.FoundationXmlGeneratorTasklet.java

/**
 * Converts instance of ClientCaseInfoType to XML document.
 * /*from  ww  w  . ja v a2 s. c  o  m*/
 * @param clientCaseInfo
 */
private void writeFoundationXmlDocument(ClientCaseInfoType clientCaseInfo) {
    String xmlData = null;
    StringWriter sw = new StringWriter();
    String outputFilename = outputDirectory + File.separator + cancerStudyIdentifier + "_merged.xml";
    try {
        JAXBContext context = JAXBContext.newInstance(clientCaseInfo.getClass());
        Marshaller jaxbMarshaller = context.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.marshal(clientCaseInfo, new File(outputFilename));
        xmlData = sw.toString();
        LOG.info("Merged Foundation XML document written to: " + outputFilename);
    } catch (JAXBException ex) {
        LOG.error("Error marshalling XML document from client case info object");
    }
}

From source file:com.jsmartframework.web.manager.WebContext.java

/**
 * Write response directly as XML from Object. Note that by using this method the response
 * as HTML will not be generated and the response will be what you have defined.
 *
 * @param object Object to convert into XML to write in the response.
 * @param marshaller JAXBContext marshaller to write object as XML.
 * @throws IOException//from  w w w.  j  av  a  2  s. c o m
 */
public static void writeResponseAsXml(Object object, Marshaller marshaller) throws IOException, JAXBException {
    WebContext context = getCurrentInstance();
    if (context != null) {
        context.responseWritten = true;
        context.response.setContentType("application/xml");
        PrintWriter writer = context.response.getWriter();
        marshaller.marshal(object, writer);
        writer.flush();
    }
}

From source file:demo.wseventing.CreateSubscriptionServlet.java

public String convertJAXBElementToStringAndEscapeHTML(Object o) throws JAXBException {
    JAXBContext jc = JAXBContext.newInstance(Subscribe.class.getPackage().getName());
    Marshaller m = jc.createMarshaller();
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    m.marshal(o, baos);
    String unescaped = baos.toString();
    return StringEscapeUtils.escapeHtml4(unescaped);
}

From source file:org.castor.jaxb.allannotations.XmlValueTest.java

@Test
public void testFullCycle() throws JAXBException {
    JAXBContext context = JAXBContext.newInstance(ForXmlValue.class);
    Marshaller m = context.createMarshaller();
    StringWriter sw = new StringWriter();
    ForXmlValue fxv = new ForXmlValue();
    fxv.content = new BigDecimal(4711);
    m.marshal(fxv, sw);
    System.out.println(sw.toString());
}

From source file:jails.http.converter.xml.Jaxb2RootElementHttpMessageConverter.java

@Override
protected void writeToResult(Object o, HttpHeaders headers, Result result) throws IOException {
    try {/*w ww .  j  ava 2  s.  c o  m*/
        Class clazz = ClassUtils.getUserClass(o);
        Marshaller marshaller = createMarshaller(clazz);
        setCharset(headers.getContentType(), marshaller);
        marshaller.marshal(o, result);
    } catch (MarshalException ex) {
        throw new HttpMessageNotWritableException("Could not marshal [" + o + "]: " + ex.getMessage(), ex);
    } catch (JAXBException ex) {
        throw new HttpMessageConversionException("Could not instantiate JAXBContext: " + ex.getMessage(), ex);
    }
}

From source file:org.mule.modules.rest.model.LeagueTransformers.java

@Transformer(resultMimeType = "text/xml")
public String toXml(League league) throws IOException, JAXBException {
    JAXBContext context = JAXBContext.newInstance(League.class);

    Marshaller m = context.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    ByteArrayOutputStream boas = new ByteArrayOutputStream();
    m.marshal(league, boas);

    return new String(boas.toByteArray());
}

From source file:org.geosdi.geoplatform.connector.server.request.CatalogCSWRequest.java

@Override
protected HttpEntity preparePostEntity()
        throws IllegalParameterFault, JAXBException, UnsupportedEncodingException {

    Marshaller marshaller = getMarshaller();

    Object request = this.createRequest();
    StringWriter writer = new StringWriter();
    marshaller.marshal(request, writer);

    return new StringEntity(writer.toString(), ContentType.APPLICATION_XML);
}

From source file:org.osmsurround.ra.export.GpxExport.java

private void marshalData(GpxType gpxType, OutputStream os) {
    try {/*ww  w  .j a v a 2s  .  c  o  m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(GpxType.class);
        Marshaller marshaller = jaxbContext.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        marshaller.marshal(of.createGpx(gpxType), os);
    } catch (JAXBException e) {
        throw new AnalyzerException(e);
    }
}

From source file:org.geosdi.geoplatform.connector.server.request.WFSRequest.java

@Override
protected HttpEntity preparePostEntity()
        throws IllegalParameterFault, JAXBException, UnsupportedEncodingException {

    Marshaller marshaller = this.getMarshaller();

    Object request = this.createRequest();
    StringWriter writer = new StringWriter();
    marshaller.marshal(request, writer);

    return new StringEntity(writer.toString(), ContentType.APPLICATION_XML);
}

From source file:gov.nih.nci.cacis.ip.mirthconnect.CanonicalModelProcessor.java

/**
 * Method accepts canonical data for processing
 * //from  ww  w  .  ja  v  a2s .co  m
 * @param request CaCISRequest
 * @return response CaCISResponse
 * @throws AcceptCanonicalFault Fault
 * @throws
 */
@WebResult(name = "caCISResponse", targetNamespace = CACIS_NS, partName = "parameter")
@WebMethod
public gov.nih.nci.cacis.CaCISResponse acceptCanonical(
        @WebParam(partName = "parameter", name = "caCISRequest", targetNamespace = CACIS_NS) CaCISRequest request)
        throws AcceptCanonicalFault {

    final CaCISResponse response = new CaCISResponse();

    final StringWriter sw = new StringWriter();
    try {
        final JAXBContext jc = JAXBContext.newInstance(CaCISRequest.class);
        final Marshaller m = jc.createMarshaller();

        final PrintWriter pw = new PrintWriter(sw);
        m.marshal(request, pw);
        response.setStatus(ResponseStatusType.SUCCESS);
    } catch (JAXBException jaxE) {
        throw new AcceptCanonicalFault("Error Marshalling object", jaxE);
    }

    try {
        String mcResponse = webServiceMessageReceiver.processData(sw.toString());

        if (mcResponse != null && (mcResponse.indexOf("Error") > -1 || mcResponse.indexOf("Exception") > -1
                || mcResponse.indexOf("ERROR") > -1 || mcResponse.indexOf("error") > -1)) {
            mcResponse = StringUtils.remove(mcResponse, "SUCCESS:");
            String channelUid = StringUtils.substringBetween(mcResponse, "(", ")");
            if (channelUid != null) {
                mcResponse = StringUtils.remove(mcResponse, "(" + channelUid + ")");
            }
            throw new AcceptCanonicalFault(
                    StringUtils.substring(mcResponse, StringUtils.lastIndexOf(mcResponse, ':')));
        }
        response.setStatus(ResponseStatusType.SUCCESS);
        return response;
        // CHECKSTYLE:OFF
    } catch (Exception e) {
        // CHECKSTYLE:ON
        //throw new AcceptCanonicalFault("Error processing message!" + e.getMessage(), e);
        throw new AcceptCanonicalFault(e.getMessage(), e);
    }
}