Skip to main content Skip to complementary content

Performing JavaBean to XML Serialization

To migrate your existing routes to the XML Representation in Talend Data Mapper, you need to use XStream.

Before you begin

Understand how the route differs in the previous implementation from the new implementation.
You start with the following route:
After migration, the route looks as such:
where cProcessor1p and cProcessor2p are additional camel processors.

Procedure

  1. Make a copy of the map used by the cMap component. In this example, name it as mXml.
  2. Add the XML Representation to the input and output structures used by mXml.
  3. Edit mXml and change the input and output representations to XML.
  4. Add xstream.jar, xpp3.jar, and xmlpull.jar to your configuration.
  5. Add the code to cProcessor1p. For example:
    org.apache.camel.Message message = exchange.getIn();
    Object payload = message.getBody();
    org.talend.transform.bean.test.Parent p = (org.talend.transform.bean.test.Parent) payload;
    
    com.thoughtworks.xstream.XStream xstream = new com.thoughtworks.xstream.XStream();
    xstream.alias("Parent", org.talend.transform.bean.test.Parent.class);
    xstream.alias("children", org.talend.transform.bean.test.Child.class);
    xstream.addImplicitCollection(org.talend.transform.bean.test.Parent.class, "children");
    String xml = xstream.toXML(p);
    
    org.apache.camel.impl.DefaultMessage in = new org.apache.camel.impl.DefaultMessage();
    in.setBody(xml);
    exchange.setIn(in);
    Information noteNote:
    • Replace org.talend.transform.bean.test with your package or classes.
    • XStream requires some aliases to generate an XML that fits the one expected by mXml.
  6. Add the code to the cProcessor2p. For example:
    org.apache.camel.Message message = exchange.getIn();
    Object payload = message.getBody();
    String xml = (String) payload;
    System.out.println(xml);
    		
    com.thoughtworks.xstream.XStream xstream = new com.thoughtworks.xstream.XStream();
    xstream.alias("Parent", org.talend.transform.bean.test.Parent.class);
    xstream.alias("children", org.talend.transform.bean.test.Child.class);
    xstream.addImplicitCollection(org.talend.transform.bean.test.Parent.class, "children");
    org.talend.transform.bean.test.Parent p = (org.talend.transform.bean.test.Parent) xstream.fromXML(xml);
    org.apache.camel.Message in = new org.apache.camel.impl.DefaultMessage();
    in.setBody(p);
    exchange.setIn(in);
    Information noteNote: Replace org.talend.transform.bean.testwith your package or classes.

Did this page help you?

If you find any issues with this page or its content – a typo, a missing step, or a technical error – let us know how we can improve!