Java class for Phone to produce <phone type="primary">317-574-7964</phone>

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

Java class for Phone to produce <phone type="primary">317-574-7964</phone>

Cocoshu
Hi,

I created a java Phone class

public class Phone {
  String type;
  String number;
 
  public Phone(String type, String number) {
    this.type = type;
    this.number = number;
  }
}

Could we instruct XStream somehow to serialize the above to the following?

<phone type="primary">317-574-7964</phone>

Thanks.

--Mircea

Reply | Threaded
Open this post in threaded view
|

Re: Java class for Phone to produce <phone type="primary">317-574-7964</phone>

Nathan Maves
yes,  use a custom converter for your Phone.class


On Jan 12, 2006, at 9:48 AM, Mircea Cocosila wrote:

> Hi,
>
> I created a java Phone class
>
> public class Phone {
>   String type;
>   String number;
>
>   public Phone(String type, String number) {
>     this.type = type;
>     this.number = number;
>   }
> }
>
> Could we instruct XStream somehow to serialize the above to the  
> following?
>
> <phone type="primary">317-574-7964</phone>
>
> Thanks.
>
> --Mircea
>

Reply | Threaded
Open this post in threaded view
|

Re: Java class for Phone to produce <phone type=

Cocoshu
Nathan Maves <Nathan.Maves@...> writes:

>
> yes,  use a custom converter for your Phone.class
>

Thanks Nathan. I read most of the postings. It seems that for many particular
needs the solution is to write a custom converter.

But how? Could you please provide a pointer to an example of that magic custom
converter?

--Mircea



Reply | Threaded
Open this post in threaded view
|

Re: Java class for Phone to produce <phone type=

Jörg Schaible-2
Mircea Cocosila wrote:

> Nathan Maves <Nathan.Maves@...> writes:
>
>>
>> yes,  use a custom converter for your Phone.class
>>
>
> Thanks Nathan. I read most of the postings. It seems that for many
> particular needs the solution is to write a custom converter.
>
> But how? Could you please provide a pointer to an example of that magic
> custom converter?

http://svn.xstream.codehaus.org/trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/ColorConverter.java?view=markup

See other classes of the converter packages for more.

- Jörg

Reply | Threaded
Open this post in threaded view
|

Re: Re: Java class for Phone to produce <phone type=

Grégory Joseph
> >  It seems that for many
> > particular needs the solution is to write a custom converter.
> >
> > But how? Could you please provide a pointer to an example of that magic
> > custom converter?
>
> http://svn.xstream.codehaus.org/trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/ColorConverter.java?view=markup
>
> See other classes of the converter packages for more.

Seems like a FAQ entry is in order ;-)

g
Reply | Threaded
Open this post in threaded view
|

Re: Re: Java class for Phone to produce <phone type=

Jörg Schaible-2
Grégory Joseph wrote:

>> See other classes of the converter packages for more.
>
> Seems like a FAQ entry is in order ;-)

This one and the attribute question :))

- Jörg

Reply | Threaded
Open this post in threaded view
|

Re: Java class for Phone to produce <phone type=

Cocoshu
In reply to this post by Jörg Schaible-2
Jörg Schaible <joerg.schaible@...> writes:

> > But how? Could you please provide a pointer to an example of that magic
> > custom converter?
>
>
http://svn.xstream.codehaus.org/trunk/xstream/src/java/com/thoughtworks/xstream/converters/extended/ColorConverter.java?view=markup
>
> See other classes of the converter packages for more.
>
> - Jörg
>
>

Thanks a lot, Jorg. Sorry for the replacer "o" in your name.

Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: Java class for Phone to produce <phone type=

João Kreuzberg
In reply to this post by Jörg Schaible-2
Hi...
 
there´s any way
to convert a java class in a xml
with attributes...
 
ex.
 
public class Person{
   private String name;
   private String lastName;
 
   public String getName(){
      return this.name;
   }
 
  public void setName(String name){
     this.name = name;
  }
 
   public String getLastName(){
      return this.lastName;
   }
 
  public void setName(String lastName){
     this.lastName= lastName;
  }
 
}
 
XStream generates this code.....
 
 
<Person>
  <name>João</name>
  <lastName>Kreuzberg</lastName>
</Person>
 
and i would like to generate like with attributes like this....
 
<Person name="João" lastName="Kreuzberg" />
 
thanks
 
João
 
Reply | Threaded
Open this post in threaded view
|

RE: Re: Re: Java class for Phone to produce <phone type=

Jörg Schaible
In reply to this post by Cocoshu
 
Hi Joao,

________________________________

        From: João Kreuzberg
        Sent: Friday, January 13, 2006 10:42 AM
        Subject: Re: [xstream-user] Re: Re: Java class for Phone to produce <phone type=
       
       
        Hi...
         
        there´s any way
        to convert a java class in a xml
        with attributes...

[snip]
         
        and i would like to generate like with attributes like this....
         
        <Person name="João" lastName="Kreuzberg" />
         
        thanks
         
        João
------------

Only with custom converters. Here's an example of how to do it:
http://article.gmane.org/gmane.comp.java.xstream.user/825
But be aware that this might have sie effects:
http://article.gmane.org/gmane.comp.java.xstream.user/1223

In general, it's good practice to search the mail archives,
as you'll find answers to a lot of questions.
There are several archives available -
http://gmane.org offers  both web and newsgroup access.

- Jörg
Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: Java class for Phone to produce <phone type=

João Kreuzberg
Thank you very much..... :D

On 1/13/06, Jörg Schaible <[hidden email]> wrote:

Hi Joao,

________________________________

       From: João Kreuzberg
       Sent: Friday, January 13, 2006 10:42 AM
       Subject: Re: [xstream-user] Re: Re: Java class for Phone to produce <phone type=


       Hi...

       there´s any way
       to convert a java class in a xml
       with attributes...

[snip]

       and i would like to generate like with attributes like this....

       <Person name="João" lastName="Kreuzberg" />

       thanks

       João
------------

Only with custom converters. Here's an example of how to do it:
http://article.gmane.org/gmane.comp.java.xstream.user/825
But be aware that this might have sie effects:
http://article.gmane.org/gmane.comp.java.xstream.user/1223

In general, it's good practice to search the mail archives,
as you'll find answers to a lot of questions.
There are several archives available -
http://gmane.org offers  both web and newsgroup access.

- Jörg