Example usage for org.springframework.oxm.jaxb Jaxb2Marshaller marshal

List of usage examples for org.springframework.oxm.jaxb Jaxb2Marshaller marshal

Introduction

In this page you can find the example usage for org.springframework.oxm.jaxb Jaxb2Marshaller marshal.

Prototype

@Override
    public void marshal(Object graph, Result result) throws XmlMappingException 

Source Link

Usage

From source file:org.cleverbus.common.Tools.java

/**
 * Marshals object graph into XML./*from ww  w  .  jav a  2s .  com*/
 *
 * @param obj the object graph
 * @param sourceClass the input class
 * @return XML as string
 * @see XmlConverter
 */
public static String marshalToXml(Object obj, Class sourceClass) {
    Jaxb2Marshaller jaxb2 = new Jaxb2Marshaller();
    jaxb2.setContextPath(sourceClass.getPackage().getName());

    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);

    jaxb2.marshal(obj, result);

    return writer.toString();
}

From source file:com.hp.autonomy.types.idol.AbstractParsingTest.java

@Test
public void symmetry() throws IOException, SAXException {
    final ResponseParser responseParser = getResponseParser(marshallerFactory, type);
    final Autnresponse autnresponse = responseParser
            .parseResponse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));

    final Jaxb2Marshaller jaxb2Marshaller = new Jaxb2Marshaller();
    jaxb2Marshaller.setClassesToBeBound(Autnresponse.class, type);

    final String generatedXml;
    try (final ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        jaxb2Marshaller.marshal(autnresponse, new StreamResult(outputStream));
        generatedXml = new String(outputStream.toByteArray(), StandardCharsets.UTF_8);
    }//from  www .ja v  a  2 s  .  com

    // Void equates to an empty responsedata element which we can't regenerate
    if (!Void.class.equals(type)) {
        XMLUnit.setIgnoreWhitespace(true);
        XMLUnit.setIgnoreComments(true);
        XMLUnit.setIgnoreAttributeOrder(true);
        final Diff diff = new Diff(xml, generatedXml);
        diff.overrideDifferenceListener(new XMLDifferenceListener());
        assertXMLEqual(diff, true);
    }
}

From source file:org.springframework.batch.item.xml.Jaxb2NamespaceMarshallingTests.java

protected Marshaller getMarshaller() throws Exception {

    Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
    marshaller.setClassesToBeBound(new Class<?>[] { QualifiedTrade.class });
    marshaller.afterPropertiesSet();/*www . j a v a2 s. c o  m*/

    StringWriter string = new StringWriter();
    marshaller.marshal(new QualifiedTrade("FOO", 100, BigDecimal.valueOf(10.), "bar"),
            new StreamResult(string));
    String content = string.toString();
    assertTrue("Wrong content: " + content, content.contains("<customer>bar</customer>"));
    return marshaller;
}