Re: Data::Dumper for Java?
From: Alan Gutierrez (alan_at_ljubljana.agtrz.com)
Date: 12/08/04
- Next message: Gerbrand van Dieijen: "Re: JDO Question : Is it possible to get unique values like DISTINCT in SQL?"
- Previous message: Michael Borgwardt: "Re: A little optimization question: Local var allocation"
- Maybe in reply to: Tim Tyler: "Re: Data::Dumper for Java?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Wed, 08 Dec 2004 04:29:17 -0600
On 2004-11-30, Sam <nnospamm@fronti.org> wrote:
> Hello,
>
> if I have something like
>
> whatever_class inst = new whatever_class();
> inst.member_1 = value_1;
> ..
> inst.member_n = value_n;
>
> is there any possibility to find out (without explizit asking) what the
> types and/or values from inst ?
>
> I have to say that I'm new to JAVA and I was wondering if there is something
> similar that I know from Perl with the module Data::Dumper.
>
> e.g. in Perl:
> Data::Dumper($inst) would result in something like that
> { member_1 => value_1,
> ....
> member_n => value_n
> }
>
> I doubt that there is something similar available (or even possible) but I
> just wanted to ask.
As mentioned elsewhere, you can use the facilities of the
java.lang.reflect package to determine the types and values of inst.
Reflection is probably what you want.
http://java.sun.com/docs/books/tutorial/reflect/
If for some reason, you really need the data to be in a string
format, then I recommend using the XStream library to serialize the
objects to XML, then you can use W3DOM, DOM4J, SAX, or XPath to poke
around a text repesentation of the document. Actually, I'd recommend
using DOM4J since it supports XPath and a Java friendly tree API.
http://xstream.codehaus.org/
http://www.dom4j.org/
(And try to use Java naming conventions, and I promise I'll use
underbars in my Perl code.)
-- Alan Gutierrez - alan@engrm.com
- Next message: Gerbrand van Dieijen: "Re: JDO Question : Is it possible to get unique values like DISTINCT in SQL?"
- Previous message: Michael Borgwardt: "Re: A little optimization question: Local var allocation"
- Maybe in reply to: Tim Tyler: "Re: Data::Dumper for Java?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|