Keith-

Ah, it really does work :) This is one of those things I knew should work, but had never worked for me... (shows how well digging through someone else's code measures up to real experience)

Speaking of stuff like this, has anyone put together a "Castor Best Practices" with common use cases? I know there are some examples in the mapping documentation, and scattered through the list archive... (eek -- that sounded dangerously close to volunteering!)

In any case, thanks for your help!  It is very much appreciated.

Stephen


Keith Visco wrote:

Stephen,


Stephen Bash wrote:

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)


Unmarshaller unm = new Unmarshaller(ArrayList.class);
unm.setMapping(mapping);
ArrayList list = (ArrayList) unm.unmarshal(reader);


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


Marshaller m = new Marshaller(myWriter);
m.setMapping(mapping);
m.setRootElement("foo");
m.marshal(list);



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


Well the above should be good, but I'll keep reading :-)


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.


Well renaming the root can be done with the Marshaller#setRootElement() method, adding the num-items attribute will have to be done with a mapping file...so in that case you'll have to create you own mapping for java.util.ArrayList. Something like the following should work (though I actually haven't tried it so it may need some tweaking) :

<class name="java.util.ArrayList">
  <map-to xml="resources"/>
  <field name="items" type="java.lang.Object"
         collection="array" get-method="toArray" set-method="add">
     <bind-xml auto-naming="deriveByClass"/>
  </field>
  <field name="size" get-method="size">
     <bind-xml name="num-items" node="attribute"/>
  </field>
</class>


Also on a side note, the xsi:type looks weird to me -- in this case is it redundant?


Yeah, I think the xsi:type is redundant here, but hopefully it doesn't hurt the unmarshalling process.

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.


Well you can pass in the ArrayList in the constructor and then simply pass in the mapping file in the setMapping method. We should probably add a setRootClass(Class root) method to be consistent. When I originally wrote the Unmarshaller it was meant to know the root class ahread of time which is why it appears in the constructor. Only later did we add the support for searching the mapping file for the root class based on the root element name.

Hope that helps,

--Keith




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]
-------------------------------------------------




-------------------------------------------------
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