Keith-

Okay, after writing this whole e-mail, I think the short summary is I don't know how to do two things:

1) Unmarshal a collection of mapped objects (no non-static Unmarshaller methods accept a class argument)

2) Customize the appearance/attributes of the root element when marshaling a collection

If there are quick answers to those two questions, feel free to skip the rest of the e-mail :) Thanks!

More detailed e-mail follows:

Hopefully Ali can fill you in on some more of the details, but I'll include some quick work I've done and some problems I've run into. Using this mapping file (referencing the class design in Ali's first e-mail):

<mapping>
  <class name="castor.emami.one.Item">
    <map-to xml="resource" />

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

    <field name="name" type="string">
      <bind-xml name="name" node="attribute" />
    </field>
  </class>
</mapping>

I get the following XML:

<?xml version="1.0" encoding="UTF-8"?>
<array-list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>
  <resource item="Item1" name="Test1" xsi:type="resource"/>
  <resource item="Item2" name="Test2" xsi:type="resource"/>
  <resource item="Item3" name="Test3" xsi:type="resource"/>
</array-list>

From Ali's first e-mail, it appears he wants to rename the root element, and he wants a "num-items" attribute in the root element. Also on a side note, the xsi:type looks weird to me -- in this case is it redundant?

Then on unmarshalling, I want to specify the mapping file, but there isn't a non-static method that can take in ArrayList.class as you mention.

Any ideas?  Thanks for the help!

Stephen

Keith Visco wrote:
Ali, Stephen,

Can you guys re-summarize the problem for me as I'm not exactly sure what the issue is that you are having. I just marshalled an ArrayList as the root element without any problems...unmarshalling also worked fine...just have to pass in ArrayList.class to the unmarshaller if xsi:type does not exist on the element.


--Keith



Stephen Bash wrote:

Ali-

I have to admit, I have never found a good solution to this problem. I almost always wrap the ArrayList inside another class, at which point you can take advantage of the collection="arraylist" attribute in a mapping file (and use nested class mappings if necessary).

One thought I do have, if (large caveat) you don't need to ever unmarshall this list back into a Java object, is to list a field in the mapping file (call it whatever) where you specify the get-method="toArray", the type="Item", and the collection="array". At that point Castor should recognize the Item class and use whatever mapping you supply for it. Unfortunately, ArrayList doesn't have a method "fromArray" or a constructor that takes in an array in order to recreate the object, making this method unusable for unmarshalling.

One other possibility might be to use custom field handlers (documentation on the Castor website), but I'm having a hard time figuring out a slick way to approach the problem from that angle.

Perhaps someone else can shed some more light on any of these options.

HTH,
Stephen

Ali Emami wrote:

Hi everybody,

Lets say that I have a ArrayList and I want to marshall it to a xml file
with a mapping file so I can get a result like this:
<resources num-items= x >
    <resource id=1/>
    <resource id=2/>
    <resource id=3/>
    <resource id=4/>
    .
    .

</resources>
And lets say my object is something like this Public Class item {
 String item,
 String name,

//Setters and getters
}

Is it possible to do this?
I could get the num-items to work by doing something like this

<class name="java.util.ArrayList" auto-complete="false">
    <map-to xml="resources"/>
    <field name="size" get-method="size">
      <bind-xml name="num-items" node="attribute"/>
    </field>
.
.
.


But I can not customize the object fields
Thanks for your help

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

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



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

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




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

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



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

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

Reply via email to