You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@cxf.apache.org by Brad O'Hearne <br...@neurofire.com> on 2007/10/24 02:53:51 UTC

Serialization of XML attributes to Java Types

Hello,

I am using CXF HTTP RESTful invocations, and I have been successfully 
using the default serialization in CXF to convert XML such as the following:

<MyObject>
    <myVar1>value 1</myVar1>
    <myVar2>value 2</myVar2>
</MyObject>

to a Java object such as:

public class MyObject {

    private String myVar1;
    private String myVar2;

    // <setters and getters for myVar1 and myVar2
    ...
}

What I would like to do, is change the serialization so that attributes 
map to Java properties, so that using the same Java object above, the 
following XML would properly map using CXF:

<MyObject myVar1="value 1" myVar2="value 2" />

Can someone enlighten me as to how this is done? That is step 1. The 
second step is to map other object types to properties, as in:

<MyObject>
    <myChildObject>
        <myVar1>value 1</myVar1>
        <myVar2>value 2</myVar2>
    </myChildObject>
</MyObject>


mapping to these Java classes:


public class MyObject {

    private MyChildObject myChildObject;

    // <setter and getter for myChildObject
    ...
}

public class MyChildObject {

    private String myVar1;
    private String myVar2;

    // <setters and getters for myVar1 and myVar2
    ...
}

Any help would be greatly appreciated!

Thanks,

Brad

RE: Serialization of XML attributes to Java Types

Posted by "Liu, Jervis" <jl...@iona.com>.
CXF HTTP REST binding uses JAXB data binding as the underlying marshal/unmarshal mechanism. I have not tried this by myself, but your case should be a matter of tuning JAXB to do a customized mapping for you. For example, if you want to map <MyObject myVar1="value 1" myVar2="value 2" /> to MyObject java class, annotating "private String myVar1" with @XmlAttribute annotation should do the job. 

Cheers,
Jervis

> -----Original Message-----
> From: Brad O'Hearne [mailto:brado@neurofire.com]
> Sent: 2007?10?24? 8:54
> To: cxf-user@incubator.apache.org
> Subject: Serialization of XML attributes to Java Types
> 
> 
> Hello,
> 
> I am using CXF HTTP RESTful invocations, and I have been successfully 
> using the default serialization in CXF to convert XML such as 
> the following:
> 
> <MyObject>
>     <myVar1>value 1</myVar1>
>     <myVar2>value 2</myVar2>
> </MyObject>
> 
> to a Java object such as:
> 
> public class MyObject {
> 
>     private String myVar1;
>     private String myVar2;
> 
>     // <setters and getters for myVar1 and myVar2
>     ...
> }
> 
> What I would like to do, is change the serialization so that 
> attributes 
> map to Java properties, so that using the same Java object above, the 
> following XML would properly map using CXF:
> 
> <MyObject myVar1="value 1" myVar2="value 2" />
> 
> Can someone enlighten me as to how this is done? That is step 1. The 
> second step is to map other object types to properties, as in:
> 
> <MyObject>
>     <myChildObject>
>         <myVar1>value 1</myVar1>
>         <myVar2>value 2</myVar2>
>     </myChildObject>
> </MyObject>
> 
> 
> mapping to these Java classes:
> 
> 
> public class MyObject {
> 
>     private MyChildObject myChildObject;
> 
>     // <setter and getter for myChildObject
>     ...
> }
> 
> public class MyChildObject {
> 
>     private String myVar1;
>     private String myVar2;
> 
>     // <setters and getters for myVar1 and myVar2
>     ...
> }
> 
> Any help would be greatly appreciated!
> 
> Thanks,
> 
> Brad
> 

----------------------------
IONA Technologies PLC (registered in Ireland)
Registered Number: 171387
Registered Address: The IONA Building, Shelbourne Road, Dublin 4, Ireland