简体   繁体   中英

Alternative for toString() in java

What I have is an unmarshalled java object from XML. The parts of this unmarshalled object is sent to various class for processing. I need to log the contents of the unmarshalled object. I can easily override the toString() method but the problem is that in this case the classes are generated at build time using xjc and I cannot override the toString() in those classes as they will be removed when generated again.

I am looking for some other alternative to print the contents. Any Idea ?

您可以使用xjc插件生成所需的toString。

Don't use toString() for marshalling. That's not what it is intended for. (Refer to the javadocs for java.lang.Object for a guide to what that method is intended for.)

Even if you do decide to implement marshalling / unmarshalling by hand, you should not attempt to use toString() for this purpose.

您可以编写一个实用程序类,该实用程序类使用反射来检查对象并返回它的字符串表示形式(所需的任何格式),或者使它直接记录字符串表示形式。

编写一个帮助程序类,该类使用未编组的Java对象并使用反射为您提供字符串表示形式,如果您不想使用反射,因为您知道xsd和未编组的内容,则可以对其进行硬编码。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM