Your first binding:

<binding direction="input">
 <mapping name="sample" class="com.sample.Sample">
   <value name="first" field="first" />
   <collection name="repeater" usage="optional"
add-method="addIgnored">
     <structure>
       <value name="elem1" />
       <value name="elem2" />
     </structure>
   </collection>
   <value name="second" field="second" />
 </mapping>
</binding>

is saying that you have an optional collection contained within an element named "repeater", but the structure inside the collection element doesn't have a name or type. This is really an error in the definition, but isn't currently being checked - the binding compiler should require both a name and a type on every child of a <collection> (unless there's an item-type on the collection element itself that defines this), since that's what was intended by design.

Your second binding:

<binding direction="input">
<mapping name="sample" class="com.sample.Sample">
<value name="first" field="first" />
<collection usage="optional" add-method="addIgnored">
<structure name="repeater"> <value name="elem1" />
<value name="elem2" />
</structure>
</collection>
<value name="second" field="second" />
</mapping>
</binding>


is saying you have an optional collection of elements named "repeater", but you still don't have any object corresponding to the structure. Here again, this should really be checked and give an error message (rather than dying obscurely).

To ignore a single optional element when unmarshalling you can just use an empty <structure> element:

 <structure name="repeater" usage="optional"/>

I don't think this will work within a collection, though. I guess the best you could do would be to write a simple custom unmarshaller and reference it as an optional element, then in the custom unmarshaller have the isPresent() method check for instances of the element and just throw them away (using the UnmarshallingContext.parsePastElement() method, or a simple loop as in org.jibx.extras.DiscardElementMapper). If you do this and use the IAliasable interface it'll allow the element name to be set in the binding, making it fully general purpose. That'd make a nice addition to the org.jibx.extras package. The only drawback to this is that you'll need to fake that the unmarshaller could return an object, so you'd still need to define a dummy set-method.

 - Dennis

Davies, Joshua wrote:

Hi, there - sorry for the long post, but XML unmarshalling issues tend
to be fairly complex; TIA to anybody who actually reads through this
whole thing.

I'm trying to use Jibx (beta 3c) to deal with an XML structure which
might or might not contain repeating elements in the middle, so a sample
might look like this:

<sample>
 <first>this is the first element</first>
 <repeater>
   <elem1>value1</elem1>
   <elem2>value2</elem2>
 <repeater>
 <repeater>
   <elem1>value3</elem1>
   <elem2>value4</elem2>
 <repeater>
 <second>this is the second element</second>
</sample>

Or:

<sample>
 <first>this is the first element</first>
 <second>this is the second element</second>
</sample>

The catch is, I don't really care about the middle (repeating) elements
in this case, so I want to skip over them.  My original jibx binding
looked like this (the add-method was added to get around a jibx compiler
error):

<binding direction="input">
 <mapping name="sample" class="com.sample.Sample">
   <value name="first" field="first" />
   <collection name="repeater" usage="optional"
add-method="addIgnored">
     <structure>
       <value name="elem1" />
       <value name="elem2" />
     </structure>
   </collection>
   <value name="second" field="second" />
 </mapping>
</binding>

This actually fails with an "IllegalStateException" at link time (stack
trace below slightly modified to make it more readable):

java.lang.IllegalStateException: Stack size mismatch on branch
in method com.sample.Sample.JiBX_sample_binding_unmarshal
generated by [EMAIL PROTECTED]
from stack:
 0: com.sample.Sample
 1: java.lang.String
to stack:
 0: com.sample.Sample

at
org.jibx.binding.classes.BranchWrapper.setTarget(BranchWrapper.java:184)
at
org.jibx.binding.classes.BranchWrapper.setTarget(BranchWrapper.java:201)
at
...def.NestedCollection.genContentUnmarshal(NestedCollection.java:134)
at ...def.ElementWrapper.genContentUnmarshal(ElementWrapper.java:272)
at ...def.NestedStructure.genContentUnmarshal(NestedStructure.java:150)
at ...def.ObjectBinding.genUnmarshalContentCall(ObjectBinding.java:611)
at ...def.ObjectBinding.genContentUnmarshal(ObjectBinding.java:723)
at ...def.ElementWrapper.genContentUnmarshal(ElementWrapper.java:272)
at ...def.MappingDefinition.generateCode(MappingDefinition.java:498)
at ...def.DefinitionContext.generateCode(DefinitionContext.java:600)
at ...def.BindingDefinition.generateCode(BindingDefinition.java:555)
at org.jibx.binding.Compile.compile(Compile.java:299)
at org.jibx.binding.Compile.main(Compile.java:364)
Exception in thread "main"

So I switched things around like this:

<binding direction="input">
<mapping name="sample" class="com.sample.Sample">
<value name="first" field="first" />
<collection usage="optional" add-method="addIgnored">
<structure name="repeater"> <value name="elem1" />
<value name="elem2" />
</structure>
</collection>
<value name="second" field="second" />
</mapping>
</binding>


But I get essentially the same error.

It seems to me that there's enough context in the definition to get
around this, so I must be missing something. Any thoughts?




-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click
_______________________________________________
jibx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to