Re: Data::Dumper for Java?

From: Alan Gutierrez (alan_at_ljubljana.agtrz.com)
Date: 12/08/04


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


Relevant Pages

  • Re: notifying particular thread to wake up.
    ... are considered to be affiliated with any sites they recommend until ... asserted that the OP had an affiliation with Sardyo (or whatever it ... it is a necessary part of every Java programmer's library and that we should ... The first response still failed ...
    (comp.lang.java.programmer)
  • Re: bug in msxml? confirmation requested.
    ... Sorry, don't have time for this, but do read this explanation (and all ... If I recommend one more thing -- it is quite useful when claiming that ... a particular XPath engine or XSLT processor has a bug -- to just test ... The other recommendation to play with a tool like the XPath Visualizer ...
    (microsoft.public.dotnet.xml)
  • Re: bug in msxml? confirmation requested.
    ... Dimitre Novatchev ... If I recommend one more thing -- it is quite useful when claiming that ... a particular XPath engine or XSLT processor has a bug -- to just test ... The other recommendation to play with a tool like the XPath Visualizer ...
    (microsoft.public.dotnet.xml)
  • ClassDefNotFound error with xpath.evaluate...
    ... from a foray into using xpath with java. ... I'm very new to java, but not to xml data manipulation. ... often failed to mention what imports are required). ...
    (comp.lang.java.programmer)
  • Re: notifying particular thread to wake up.
    ... are considered to be affiliated with any sites they recommend until ... The original link was provided in response to someone asking about multithreaded Java programming. ... It is also, I think, a useful book for multithreaded programming generally, not just in Java, despite its focus on Java idioms. ...
    (comp.lang.java.programmer)