[xstream-user] newbie JavaBeans, marshalling, etc

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

[xstream-user] newbie JavaBeans, marshalling, etc

Nathan Poe
I'm just jumping in to XML and how all this works in Java, and I'm running into
some major confusion.

I got the basic stuff to work using the 2 minute tutorial, and I've wandered
around this list extensively, but now I'm trying to get a JavaBean to show up
without "__".  I'm trying to use JavaBeanConverter stuff, but I'm just not
understanding the whole "marshalling" serialize etc stuff. I know its probably
really simple, but I'm just not getting it.  Could someone please post an
example of how to load/serialize/whatever (I'm not up on the lingo yet) a
JavaBean into an xstream...?  

Here's what I've got so far...

public String runXtream()
        {
someBean newBean = new SomeBean();
//sets of new Bean...

XStream xstream = new XStream();

xstream.registerConverter(new JavaBeanConverter(xstream.getClassMapper(),
"class"), -20);
       
xstream.marshal(newBean,new HierarchicalStreamWriter(???));

xstream.alias("Customer", SomeBean.class);
   
String xml = xstream.toXML(SomeBean);
return xml;
     }

I hope I've not done so many things wrong here that you all throw up your hands
in disgust :)

In particular, how do I instantiate this "HierarchicalStreamWriter"...is this
coming from somewhere else???  Any help is greatly appreciated, and again, sorry
for wasting everyone's time!

Nathan
 



Reply | Threaded
Open this post in threaded view
|

[xstream-user] Re: newbie JavaBeans, marshalling, etc

Mauro Talevi
Hi Nathan,

Nathan Poe wrote:

> I'm just jumping in to XML and how all this works in Java, and I'm running into
> some major confusion.
>
> I got the basic stuff to work using the 2 minute tutorial, and I've wandered
> around this list extensively, but now I'm trying to get a JavaBean to show up
> without "__".  I'm trying to use JavaBeanConverter stuff, but I'm just not
> understanding the whole "marshalling" serialize etc stuff. I know its probably
> really simple, but I'm just not getting it.  Could someone please post an
> example of how to load/serialize/whatever (I'm not up on the lingo yet) a
> JavaBean into an xstream...?  

you can find some examples in
https://svn.codehaus.org/xstream/trunk/xstream/src/test/com/thoughtworks/xstream/XStreamTest.java


>
> Here's what I've got so far...
>
> public String runXtream()
> {
> someBean newBean = new SomeBean();
> //sets of new Bean...
>
> XStream xstream = new XStream();
>
> xstream.registerConverter(new JavaBeanConverter(xstream.getClassMapper(),
> "class"), -20);
>
> xstream.marshal(newBean,new HierarchicalStreamWriter(???));
>
> xstream.alias("Customer", SomeBean.class);
>    
> String xml = xstream.toXML(SomeBean);
> return xml;
>      }
>
> I hope I've not done so many things wrong here that you all throw up your hands
> in disgust :)
>
> In particular, how do I instantiate this "HierarchicalStreamWriter"...is this
> coming from somewhere else???  Any help is greatly appreciated, and again, sorry
> for wasting everyone's time!
>

There is no need to do that unless you want to use custom features.
XStream is a facade that simplifies the serialisation for you.

In it's simplest form:
XStream xstream = new XStream();
SomeBean bean = new SomeBean();
xstream.toXML(bean);

If you want to handle underscores in the fields names, check out the
testUnmarshalsObjectFromXmlWithUnderscores() in XStreamTest.

Cheers

Reply | Threaded
Open this post in threaded view
|

[xstream-user] Re: newbie JavaBeans, marshalling, etc

Nathan Poe
Thanks Mauro!

....the testing code was a huge help! I was actually really close a while back,
and then just got off on a complete tangent with marshalling stuff (still don't
understand what the intended use is there...?).  For anyone that follows me, to
get "__" out of your response when using a plain old JavaBean, here's some
possible code:


//BEGIN CODE
  SomeBean newBean = new SomeBean()
  //populate someBean....
  //then...

  XStream xstream = new XStream();
  xstream.registerConverter(new JavaBeanConverter(xstream.getClassMapper(),
"class"), -20);
       
   xstream.alias("SomeBean", newBean.class);    
   String xml = xstream.toXML(newBean);
//END CODE


The only other thing I would add is that I wish i had found that test code hours
ago...!!  Does the XStream site point to that stuff from anywhere?

For anyone who wants to get to the root of that goodness:

https://svn.codehaus.org/xstream/trunk/xstream/src/test/com/thoughtworks/xstream/

Thanks again Mauro!!