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.github.woozoo73.ht.format.XmlFormat.java

public String formatInternal(Invocation invocation) throws Exception {
    JAXBContext jaxbContext = JAXBContext.newInstance(invocation.getClass());
    Marshaller marshaller = jaxbContext.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();
    marshaller.marshal(invocation, writer);

    String xml = writer.getBuffer().toString();

    return xml;// ww  w.  j a v a 2 s  .  co m
}

From source file:com.photon.phresco.plugins.util.WarConfigProcessor.java

public void save() throws JAXBException {
    JAXBContext jaxbContext = JAXBContext.newInstance(Assembly.class);
    Marshaller marshal = jaxbContext.createMarshaller();
    marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
    marshal.marshal(assembly, file);/* w  w  w  .j  av a 2 s  .c  om*/
}

From source file:org.esbtools.gateway.GatewayRequest.java

public String toXML() {
    StringWriter thisXML = new StringWriter();

    try {/*ww  w .j  av  a 2  s  . co m*/
        JAXBContext jaxbContext = JAXBContext.newInstance(this.getClass());
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        jaxbMarshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
        jaxbMarshaller.marshal(this, thisXML);

    } catch (JAXBException e) {
        throw new RuntimeException(e);
    }
    return thisXML.toString();
}

From source file:com.healthcit.cacure.businessdelegates.export.DataExportTest.java

@Test
public void testConstructFormXML() throws JAXBException {
    assertNotNull(dataExport);//from  w w w  .  j  a v  a 2 s.  co m
    JAXBContext jc = JAXBContext.newInstance("com.healthcit.cacure.export.model");
    Marshaller m = jc.createMarshaller();
    //long formId = 9712;
    //long formId = 9724;
    long formId = 9731;
    //long formId = 9833;
    Cure xml = dataExport.constructFormXML(formId);
    //Cure xml = dataExport.constructFormXML(9724);
    //Cure xml = dataExport.constructFormXML(9731);
    //m.marshal(xml, new File("C:\\temp\\caure-9724.xml"));
    m.marshal(xml, new File("C:\\temp\\cure-" + formId + ".xml"));
}

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

/**
 * Converts instance of ClientCaseInfoType to XML document.
 * //from  w ww. jav a 2 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:org.osmsurround.ra.export.GpxExport.java

private void marshalData(GpxType gpxType, OutputStream os) {
    try {/*from w w  w .  j  a  va2s  . 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:de.utkast.encoding.EncodingTestClient.java

@Test
public void testEncoding() throws Exception {

    String input = "?? ?";
    String url = "http://localhost:8080/restful-encoding/enc";

    EncodingDTO dto = new EncodingDTO(input);

    JAXBContext ctx = JAXBContext.newInstance(EncodingDTO.class);
    StringWriter writer = new StringWriter();
    ctx.createMarshaller().marshal(dto, writer);

    System.out.println(String.format("Will send: %s", writer.toString()));

    HttpClient client = HttpClientBuilder.create().build();
    HttpPost post = new HttpPost(url);
    HttpEntity entity = new ByteArrayEntity(writer.toString().getBytes("UTF-8"));
    post.setEntity(entity);/*from   ww w .  ja v a 2  s.  c o m*/
    post.setHeader("Content-Type", "application/xml;charset=UTF-8");
    post.setHeader("Accept", "application/xml;charset=UTF-8");

    HttpResponse response = client.execute(post);
    String output = EntityUtils.toString(response.getEntity(), "UTF-8");

    assertEquals(writer.toString(), output);
}

From source file:org.openmrs.module.dhisreport.api.DHIS2ReportingServiceDXFTest.java

@Ignore
@Test//from   w ww .  j  av  a2s . c o  m
public void postDhisReportTest() throws Exception {
    HttpDhis2Server server = new HttpDhis2Server();
    server.setUsername("admin");
    server.setPassword("district");
    server.setUrl(new URL("http://apps.dhis2.org/dev"));

    service.setDhis2Server(server);
    ClassPathResource resource = new ClassPathResource("dvset.xml");
    JAXBContext jaxbContext = JAXBContext.newInstance(DataValueSet.class);

    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    DataValueSet dvset = (DataValueSet) jaxbUnmarshaller.unmarshal(resource.getInputStream());
    ImportSummary summary = service.postDataValueSet(dvset);
    JAXBContext importSummaryContext = JAXBContext.newInstance(ImportSummary.class);

    Marshaller jaxbmarshaller = importSummaryContext.createMarshaller();
    jaxbmarshaller.marshal(summary, System.out);
}

From source file:org.javelin.sws.ext.bind.SweJaxbContextFactoryTest.java

@Test
public void emptySetOfClasses() throws Exception {
    JAXBContext ctx = SweJaxbContextFactory.createContext(new Class[] {}, null);
    ctx.createMarshaller().marshal(
            new JAXBElement<String>(new QName("urn:test", "str"), String.class, "content"), System.out);
}

From source file:org.jvoicexml.demo.mmi.simpledemo.SimpleMmiDemo.java

public void send(final Mmi mmi, final URI target) throws JAXBException, IOException {
    final JAXBContext ctx = JAXBContext.newInstance(Mmi.class);
    final Marshaller marshaller = ctx.createMarshaller();
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    marshaller.marshal(mmi, out);//from ww  w  . j a  v a 2 s. c  om
    final HttpClient client = new DefaultHttpClient();
    final HttpPost post = new HttpPost(target);
    final HttpEntity entity = new StringEntity(out.toString(), ContentType.APPLICATION_XML);
    post.setEntity(entity);
    client.execute(post);
    LOGGER.info("sending " + mmi + " to '" + target + "'");
}