How to obtain a String representation of my object?

Hello,

First please excuse me for my English, I'm French.

So, I have began an application which use a XML file config to start because I need to map data between 2 generic class like this:

att outgoing="Package.ClassName1.att1" att incoming="PackageClassName2.att2"

So I have employed reflection to fing get and set method to map attribute where It's String or integer but I have one case where I must give an existing object with values to an other class, for exemple:

att outgoing="Package.ClassName1.att1" att incoming="?"

So my att outgoing wait for an object with specific type. In fact, my question is, how I can represent my instantiate object with a string wich I can put in my XML file? It is possible? Because I have find some API as Xstream which serialize to an entire XML tree but it's not the easiest for me...

thank you for your answer and have a good day!

[1023 byte] By [Doctora] at [2007-11-26 21:25:13]
# 1
well, unfortunately, writing software just isn't easy. Xstream is about as easy as it gets with serializing to XML, your best bet is to persevere with that. Xstream is fairly well-documented, just stick with it :-)
georgemca at 2007-7-10 3:05:17 > top of Java-index,Core,Core APIs...
# 2
using spring you can initialize with ref beans, and for each bean, you can initialize internal variable values.
mchan0a at 2007-7-10 3:05:17 > top of Java-index,Core,Core APIs...
# 3
Ok so I must forget my primary idea to "transform" my object in string representation and I have to use XML ?mchan0, do you have some example of using Spring like you say please because I have never use Spring and I don't understand what are you talking about?
Doctora at 2007-7-10 3:05:17 > top of Java-index,Core,Core APIs...
# 4

maybe not what you want,

//Tue Mar 13 09:25:33 EDT 2007

//

//

////./Qb.java

//

//

1

2abstract class AbstractObject

3{

4}

5

6class MyObject1 extends AbstractObject

7{

8private AbstractObject current = null;

9public AbstractObject getCurrent() {

10return this.current;

11}

12public void setCurrent( AbstractObject obj ) {

13this.current = obj;

14}

15public String toString() {

16return "MyObject1 cur: " + current.toString();

17}

18}

19

20class MyObject2 extends AbstractObject

21{

22private AbstractObject another = null;

23public AbstractObject getAnother() {

24return this.another;

25}

26public void setAnother( AbstractObject obj ) {

27this.another = obj;

28}

29public String toString() {

30return "MyObject2 ano: " + another.toString();

31}

32}

33

34class MyObject3 extends AbstractObject

35{

36private String variable = null;

37public String getVariable() {

38return this.variable;

39}

40public void setVariable( String obj ) {

41this.variable = obj;

42}

43public String toString() {

44return "MyObject3 var: " + variable;

45}

46}

47

48public class Qb

49{

50

51public static void main(String[] args) throws Exception

52{

53Qb qb = new Qb();

54qb.init();

55}

56private AbstractObject myObject1 = null;

57public AbstractObject getMyObject1() {

58return this.myObject1;

59}

60public void setMyObject1( AbstractObject obj ) {

61this.myObject1 = obj;

62}

63private void init() {

64this.myObject1 = (AbstractObject) SpringBeanFactory.init("myObject1");

65System.out.println( this.myObject1 );

66AbstractObject obj2 = (AbstractObject) SpringBeanFactory.init("myObject2");

67( (MyObject1) this.myObject1 ).setCurrent( obj2 );

68System.out.println( this.myObject1 );

69}

70

71}

//

//

////./spring.xml

//

//

1<?xml version="1.0" encoding="UTF-8"?>

2<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"

3"http://www.springframework.org/dtd/spring-beans.dtd"

4>

5

6<beans>

7

8

9<bean id="myObject1" class="MyObject1">

10 <property name="current" ref="myObject3" />

11</bean>

12

13<bean id="myObject2" class="MyObject2">

14 <property name="another" ref="myObject3" />

15</bean>

16

17<bean id="myObject3" class="MyObject3">

18 <property name="variable" value="hello world" />

19</bean>

20

21</beans>

22

23

//

//

////./SpringBeanFactory.java

//

//

1

2import org.springframework.beans.factory.BeanFactory;

3import org.springframework.beans.factory.xml.XmlBeanFactory;

4import org.springframework.core.io.*;

5

6public class SpringBeanFactory

7{

8private static BeanFactory factory = null;

9

10public static Object init(String name)

11{

12try

13{

14if (null == factory)

15{

16

17factory = new XmlBeanFactory(

18new DefaultResourceLoader()

19.getResource("spring.xml"));

20}

21return factory.getBean(name);

22} catch (Exception e)

23{

24e.printStackTrace();

25}

26return null;

27}

28}

./Qb.java

./spring.xml

./SpringBeanFactory.java

output:

$ java Qb

0 [main] INFO core.CollectionFactory - JDK 1.4+ collections available

0 [main] INFO core.CollectionFactory - Commons Collections 3.x available

47 [main] INFO xml.XmlBeanDefinitionReader - Loading XML bean definitions from

class path resource [spring.xml]

MyObject1 cur: MyObject3 var: hello world

MyObject1 cur: MyObject2 ano: MyObject3 var: hello world

mchan0a at 2007-7-10 3:05:17 > top of Java-index,Core,Core APIs...
# 5
Ok mchan0, thank you very much, I will study this example but it seems to me that it can help me a lot!!!
Doctora at 2007-7-10 3:05:17 > top of Java-index,Core,Core APIs...