Hi Dennis,

Using the guidelines you provided, I have been able to map an array
directly.

I am providing the solution to the mailing list, if you see any possible
improvement, just let me know.

Here is what I have in my binding file:

binding.xml
<mapping class="Banana" name="Banana">
 ...
 Banana class mapping
 ...
</mapping>

<mapping class="Banana[]" name="Bananas"
marshaller="org.jibx.extras.TypedArrayMapper" u
unmarshaller="org.jibx.extras.TypedArrayMapper"/>

And here the the Java code, the writeArray method marshals an array of
object assuming the above mapping style is used, it could marshal a Banana[]
type :

public void writeArray(Object[] msgs, String fileName) {
 try {
   IBindingFactory bfact = BindingDirectory.getFactory(msgs[0].getClass());

   // get the index of the Array marshaller
   String klassName = msgs[0].getClass().getName()+"[]";
   int klassIndex = -1;
   for (int i = 0; i < bfact.getMappedClasses().length; i++) {
     if (klassName.equals(bfact.getMappedClasses()[i])) {
       klassIndex = i;
       break;
     }
   }
   if (klassIndex == -1) {
     throw new Exception("Array class marshaller not found:
"+klassName);
   }

   // create marshalling context, marshaller and output
   IMarshallingContext mctx = bfact.createMarshallingContext();
   IMarshaller marshaller = mctx.getMarshaller(klassIndex, klassName);
   FileOutputStream out = new FileOutputStream(fileName);

   // configure context used by marshaller
   mctx.setIndent(2);
   mctx.setOutput(out, null);

   // get Array marshaller and marshall Array
   mctx.startDocument(null, null);
   marshaller.marshal(msgs, mctx);
   mctx.endDocument();

 } catch (Exception e) {
   e.printStackTrace();
 }
}

Regards,
Stephane


On 11/9/06, Dennis Sosnoski <[EMAIL PROTECTED]> wrote:

Hi Stephane,

Mapping an array directly is not possible because of the way Java
handles arrays - each array type in Java is a separate class, but it's a
synthesized class rather than a real one. Because the class is synthetic
there's no way to add information to the class.

It's not too hard to handle this directly, though. If the root object
you're marshalling/unmarshalling is an array, just define a normal
<mapping> for the item type of the array and a custom <mapping> for the
array type using org.jibx.extras.TypedArrayMapper as the marshaller= and
unmarshaller=. You can get the factory by passing the item type, rather
than the array type. I think unmarshalling should then work
automatically. Marshalling is a little more difficult - you have to
search through the array returned by the
IBindingFactory.getMappedClasses() method to determine the mapping
index, then use that index to call the
IMarshallingContext.getMarshaller() method. You can then use the
returned IMarshaller directly.

Let me know if you run into any problems doing this.

  - Dennis

Dennis M. Sosnoski
SOA, Web Services, and XML
Training and Consulting
http://www.sosnoski.com - http://www.sosnoski.co.nz
Seattle, WA +1-425-296-6194 - Wellington, NZ +64-4-298-6117



Stephane Elie wrote:
> Hi Dennis,
>
> I have read the tutorials before and they are very clear and well
written.
>
> What I want to do is not in these tutorials though.
>
> The root of my problem lies in the fact that the <collection> tag has
> to be within a <mapping> definition, which is linked to a class.
> Basically, I want to map only a MyItem[] java array in the XML file,
> not a MyItem, nor a MyClass.MyItem[].
> So when I want to actually do my binding, I do
> BindingDirectory.getFactory( MyItem[] ), which I cannot get to work.
>
> I know how to bind an array within a class, but not an array of
> class.  Is it possible?
>
> Thanks,
> Stephane
>
> On 11/9/06, *Dennis Sosnoski* < [EMAIL PROTECTED]
> <mailto:[EMAIL PROTECTED]>> wrote:
>
>     Hi Stephane,
>
>     The tutorial section on collections gives some examples that should
be
>     relevant:
>     http://jibx.sourceforge.net/tutorial/binding-collects.html For
>     what you want, a simple binding like:
>
>       <collection field="list" name="list">
>         <structure name="item" type="MyItem">
>           <value name="field" field="field"/>
>         </structure>
>       </collection>
>
>     should do exactly what you want.
>
>     Had you looked at the binding tutorial? If this isn't clear from the
>     tutorial, I'd appreciate feedback on how to show it more clearly.
>
>       - Dennis
>
>     Dennis M. Sosnoski
>     SOA, Web Services, and XML
>     Training and Consulting
>     http://www.sosnoski.com - http://www.sosnoski.co.nz
>     <http://www.sosnoski.co.nz>
>     Seattle, WA +1-425-296-6194 - Wellington, NZ +64-4-298-6117
>
>
>
>     Stephane Elie wrote:
>     > Hi,
>     >
>     > I haven't found a way to map an array directly in the XML file.
>     >
>     > I would like to do something like this:
>     >
>     > <binding>
>     >   <!-- this works and map a single object in the XML file  -->
>     >   <mapping class=" test.MyClass" name="item">
>     >     <value name="field" field="field" />
>     >   </mapping>
>     >
>     >   <!-- I want to do something like this, but it doesn't
work...  -->
>     >   <collection item-type="test.MyClass" name="list"/>
>     > </binding>
>     >
>     > I want my XML file to look like :
>     > <list> <!-- I don't care about the name here -->
>     >   <item>
>     >     <field>value1</field>
>     >   </item>
>     >   <item>
>     >     <field>value2</field>
>     >   </item>
>     >   <item>
>     >     <field>value3</field>
>     >   </item>
>     > </list>
>     >
>     > and this maps in a Java array : test.MyClass[]
>     >
>     > Anybody knows if this is possible?
>     >
>     > Thanks,
>     >
>     > Stephane
>     >
>     > P.S.
>     > I can create a wrapper class around the array like this (but it
>     is not
>     > practical for me...):
>     > <binding>
>     >   <mapping class="test.WrapperClass" name="top_level">
>     >     <collection name="list" field="list"/>
>     >   </mapping>
>     > </binding>
>     >
>     >
>
------------------------------------------------------------------------
>     >
>     >
>
-------------------------------------------------------------------------
>     > Using Tomcat but need to do more? Need to support web services,
>     security?
>     > Get stuff done quickly with pre-integrated technology to make
>     your job easier
>     > Download IBM WebSphere Application Server v.1.0.1 based on
>     Apache Geronimo
>     >
>
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>     <
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642>
>     >
>
------------------------------------------------------------------------
>     >
>     > _______________________________________________
>     > jibx-users mailing list
>     > jibx-users@lists.sourceforge.net
>     <mailto:jibx-users@lists.sourceforge.net>
>     > https://lists.sourceforge.net/lists/listinfo/jibx-users
>     <https://lists.sourceforge.net/lists/listinfo/jibx-users>
>     >
>
>
-------------------------------------------------------------------------
>     Using Tomcat but need to do more? Need to support web services,
>     security?
>     Get stuff done quickly with pre-integrated technology to make your
>     job easier
>     Download IBM WebSphere Application Server v.1.0.1 based on Apache
>     Geronimo
>
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
>     <
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642>
>     _______________________________________________
>     jibx-users mailing list
>     jibx-users@lists.sourceforge.net
>     <mailto:jibx-users@lists.sourceforge.net>
>     https://lists.sourceforge.net/lists/listinfo/jibx-users
>
>
> ------------------------------------------------------------------------
>
>
-------------------------------------------------------------------------
> Using Tomcat but need to do more? Need to support web services,
security?
> Get stuff done quickly with pre-integrated technology to make your job
easier
> Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
> http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
> ------------------------------------------------------------------------
>
> _______________________________________________
> jibx-users mailing list
> jibx-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jibx-users
>

-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job
easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys - and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to