I have an XML file with a set of addresses (street, city, state, zip), and the corresponding java Address class. I need to have the primary address (using a primary tag) and zero or more secondary addresses (using a secondary tag). In my attempt to do this, I've come across a few minor bugs (I think).
Java files: class Person { Address primary; ArrayList secondary; }
XML File: <person> <primary> <street>124 main street</street><city>new york</city> <state>NY</state> <zip>000000</zip> </primary> <secondary> <street>142 tree street, apt 14</street> <city>Burlington</city><state>MI</state> <zip>00000</zip> </secondary> </person>
Binding attempt 1: <binding> <namespace uri="http://www.softstart.com/" default="elements" /> <mapping name="person" class="Person" field="person"> <structure field="primary" map-as="Address"/> <structure name="secondary" usage="optional"> <collection field="secondary" item-type="Address"/> </structure> </mapping> <mapping name="primary" class="Address"> <value name="street" field="street"/> <value name="city" field="city" /> <value name="state" field="state" /> <value name="zip" field="zip" /> </mapping> </binding>
This binds properly, but fails on read with unmarshalling with an:
org.jibx.runtime.JiBXException: Expected "{http://www.softstart.com/}secondary" end tag, found "{http://www.softstart.com/}street" end tag (line 5, col 39)
at org.jibx.runtime.impl.UnmarshallingContext.throwEndTagNameError(UnmarshallingContext.java:307)
at org.jibx.runtime.impl.UnmarshallingContext.parsePastEndTag(UnmarshallingContext.java:829)
I tried changing the Address mapping to abstract, and setting the name="primary" for the first structure. This fails on binding with:
[java] org.jibx.runtime.JiBXException: Name not allowed when using mapping reference at tag "structure"(line 4, col 35, in bind.xml)
[java] at org.jibx.runtime.impl.UnmarshallingContext.throwStartTagException(UnmarshallingContext.java:2725)
I tried creating two Address mappings, one with name="primary" the other with name="secondary". This fails on bind with:
[java] org.jibx.runtime.JiBXException: Conflicting mappings for class com.softstart.Address
[java] at org.jibx.binding.def.DefinitionContext.addMapping(DefinitionContext.java:183)
Getting frustrated, I tried to flatten the listing, which worked. And I discovered that the structure element will take a class attribute, even through neither the documentation nor the XSD say it's allowed. That is, the following is accepted and works as expected:
<binding> <namespace uri="http://www.softstart.com/" default="elements" /> <mapping name="person" class="Person" field="person"> <structure field="primary" name="primary" class="Address"> <value name="street" field="street"/> <value name="city" field="city" /> <value name="state" field="state" /> <value name="zip" field="zip" /> </structure> <structure usage="optional"> <collection field="secondary" item-type="Address"/> </structure> </mapping> <mapping name="secondary" class="Address"> <value name="street" field="street"/> <value name="city" field="city" /> <value name="state" field="state" /> <value name="zip" field="zip" /> </mapping> </binding>
The other way I got this to apparently work was using the label/using construct. Which has a big warning that it is being depreciated. Binding is as follows:
<binding> <namespace uri="http://www.softstart.com/" default="elements" /> <mapping name="person" class="Person" field="person"> <structure field="primary" name="primary" using="ADDRESS"/> <structure name="secondary" usage="optional"> <collection field="secondary" item-type="Address"/> </structure> </mapping> <mapping name="secondary" class="Address" label="ADDRESS"> <value name="street" field="street"/> <value name="city" field="city" /> <value name="state" field="state" /> <value name="zip" field="zip" /> </mapping> </binding>
Of course this doesn't work the other way around, with the address map for primary, and the using on the secondary structure/collection.
<binding> <namespace uri="http://www.softstart.com/" default="elements" /> <mapping name="person" class="Person" field="person"> <structure field="primary" map-as="Address"/> <structure name="secondary" usage="optional" using="ADDRESS"> <collection field="secondary" item-type="Address"/> </structure> </mapping> <mapping name="primary" class="Address" label="ADDRESS"> <value name="street" field="street"/> <value name="city" field="city" /> <value name="state" field="state" /> <value name="zip" field="zip" /> </mapping> </binding>
This returns a binding error:
[java] org.jibx.runtime.JiBXException: Expected end tag, found start tag "collection" (line 6, col 60, in bind.xml)
[java] at org.jibx.runtime.impl.UnmarshallingContext.toEnd(UnmarshallingContext.java:560)
There were a whole bunch of other interesting bad responses, though the binding was obviously wrong I had to try them anyways.
The final question: What is the proper solution to this problem (if any)?
-- Thomas Jones-Low Softstart Services Inc. [EMAIL PROTECTED] JobScheduler for Oracle Ph: 802-398-1012 http://www.softstart.com
-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
jibx-users mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jibx-users