The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

megachucky (17) [Avatar] Offline
#1
Hey,

unfortunately, there is no full example for using XStream.

I wanna do the following (and I am not sure, if it is possible without JAXB):
A bean "HelloBean.java" has two attributes with getters and setters.

public class HelloBean {

private String testAttribute;

private String oneMoreAttribute;

public HelloBean() {

this.setTestAttribute("att1");
this.setOneMoreAttribute("att2");
}

public String hello(String name) {
return "Hello: " + name;
}



I want to get an XML file, approximately:
<helloBean>
<testAttribute>att1</testAttribute>
<oneMoreAttribute>att2</oneMoreAttribute>
<helloBean>

This should work with XStream, right?

I use the following route:

from("directsmiliexx").
// bean(HelloBean.class, "hello").
bean(HelloBean.class).
marshal().xstream("UTF-8").
process(new LogUnknownOrderProcessor());

My template-producer:
template.sendBodyAndHeader("directsmiliexx", "this is a bean to xml transformation",
Exchange.BEAN_METHOD_NAME, "hello");

The output is:
BodyType:byte[], Body:<?xml version='1.0' encoding='UTF-8'?><string>Hello: this is a bean to xml transformation</string>

So, calling the hello-method works. But what route do I have to use to get the desired output:

<helloBean>
<testAttribute>att1</testAttribute>
<oneMoreAttribute>att2</oneMoreAttribute>
<helloBean>

??? Or is this not possible without JAXB?


Thanks for help in advance...

Best regards, Kai.

Message was edited by:
megachucky
davsclaus (413) [Avatar] Offline
#2
Re: Section 3.3.2: Transforming using XStream
It doesn't work because you hello method on the HelloBean return a String. So the XStream gets a String to marshal. Instead what you want is to set a bean instance as the body of the message.

For example you can change the code in hello to return a HelloBean

{code}
public HelloBean hello(String name) {
HelloBean answer = new HelloBean();
answer.setTestAttribute("Something");
return answer;
{code}

Then XStream has a HelloBean as the message body to marshal.

Also a good idea is to check the unit tests of a given Camel component. If neither the book, nor the component documentation helps you.
https://svn.apache.org/repos/asf/camel/trunk/components/camel-xstream/src/test/