That was very helpful.
Thanks a lot.

On 6/22/05, Stephen Bash <[EMAIL PROTECTED]> wrote:
Kumar-

I used this reference: http://castor.codehaus.org/xml-mapping.html to
generate the following mapping file:

    <?xml version="1.0"?>
    <!DOCTYPE ... (omitted) ...>

    <mapping>
    <class name="castor.mettu.SLIR">
    <map-to xml="slir" />

    <field name="msids" type=" castor.mettu.MSID" collection="collection">
      <bind-xml name="msid" location="msids" node="element" />
    </field>
    </class>

    <class name=" castor.mettu.MSID">
    <map-to xml="blah-blah" />

    <field name="type" type="string" direct="true">
      <bind-xml name="type" node="attribute" />
    </field>

    <field name="value" type="string" direct="true">
      <bind-xml node="text" />
    </field>
    </class>
    </mapping>

Which using code included at the end of this email generated the
following output:

    Castor Output:
    <?xml version="1.0" encoding="UTF-8"?>
    <slir>
        <msids>
            <msid type="mdn">str1</msid>
            <msid type="mdn">str2</msid>
        </msids>
    </slir>

    Reconstructed Object:
    MSID type = mdn, value = str1
    MSID type = mdn, value = str2

I believe this fits your original email.  The only major item worth
mentioning is the reference document doesn't mention a collection type
"list" as built into Castor (at least in mapping), so I used
"collection" for the field msids.  There is an "arraylist" value that
can be used in that location.  As a side note, examine mapping
definition for MSID, where I specify that it should map to an xml
element "blah-blah"; this demonstrates that Castor is using the
specified xml name for the field in SLIR not the map-to element for MSID
(I think map-to is mostly used for root elements).

Let me know if you have any more questions.  In general the mapping
files are fairly easy to figure out with a little trial and error, but
the above reference yields 99% of what is needed for all but the most
complex mappings.

Thanks,
Stephen


Driver code snippet (error handling removed):

MSID msid1 = new MSID();
msid1.type = "mdn";
msid1.value = "str1";
MSID msid2 = new MSID();
msid2.type = "mdn";
msid2.value = "str2";

List msids = new ArrayList();
msids.add( msid1 );
msids.add( msid2 );

SLIR slir = new SLIR();
slir.setMsids( msids );

Mapping myMap = new Mapping();
myMap.loadMapping( MettuDriver.class.getResource(
    "/castor/mettu/mettuMapping.xml" ) );

StringWriter myWriter = new StringWriter();
Marshaller m1 = new Marshaller( myWriter );
m1.setMapping( myMap );
m1.marshal( slir );

System.out.println( "Castor Output: " );
System.out.println ( myWriter.getBuffer().toString() );

Unmarshaller um1 = new Unmarshaller( myMap );
StringReader myReader = new StringReader(
    myWriter.getBuffer().toString() );
SLIR newSLIR = (SLIR)um1.unmarshal( myReader );

System.out.println( "Reconstructed Object: " );
Iterator iter = newSLIR.getMsids().iterator();
while( iter.hasNext() ) {
   MSID curMSID = (MSID)iter.next();
   System.out.println( "MSID type = " + curMSID.type + ",
     value = " + curMSID.value );
}


Kumar Mettu wrote:
> Hi,
>
>   I couldn't find help on how to do mapping of this file for XML to my
> java objects. I am hoping there is a better way to do this. I already
> have existing java objects for which i am hoping castor can do XML to
> Java mapping.
>
>
>     <slir>
>         <msids>
>             <msid type="mdn">str1</msid>
>             <msid type="mdn">str2</msid>
>         </msids>
>     </slir>
>
> I have the following classes:
>
> public class MSID {
>     public String type;
>     public String value;
> }
>
> public class SLIR {
>    private List msids;
>
>    public List getMsids() {
>         return msids;
>    }
>
>    public void setMsids(List msids) {
>         this.msids = msids;
>    }
> }
>
>
> Any help on mapping file is of great help.
>
> Thanks,
> Kumar.
>
>
>

-------------------------------------------------
If you wish to unsubscribe from this list, please
send an empty message to the following address:

[EMAIL PROTECTED]
-------------------------------------------------


Reply via email to