Java Mailing List Archive

http://www.junlu.com/

Home » user.castor »

[castor-user] Unmarshalling sequence with optional elements

rednix

2012-06-18

Replies: Find Java Web Hosting

Author LoginPost Reply

Hi,

I'm struggling with a mapping/unmarshalling of a XML which contains a
sequence with an optional element.

Here is the XSD part for the list:

    <xs:element name="relations" maxOccurs="1" minOccurs="0"
type="relationsType">
    </xs:element>

 <xs:complexType name="relationsType">
   <xs:annotation>
     <xs:documentation xml:lang="en">Either a set of a lead project and a
lead-sourcing scope or a set of cosourced projects with an optional sourcing
scope.</xs:documentation>
   </xs:annotation>
   <xs:choice minOccurs="1" maxOccurs="1">
     <xs:sequence minOccurs="1" maxOccurs="1">
       <xs:element name="leadProject" type="projectType" minOccurs="1"
maxOccurs="1"/>
       <xs:element name="leadSs" maxOccurs="1" minOccurs="1" type="ssRefType"/>
     </xs:sequence>
     <xs:sequence maxOccurs="500" minOccurs="1">
       <xs:element name="coProject" maxOccurs="1" minOccurs="1"
type="projectType">
       <xs:annotation>
         <xs:documentation xml:lang="en">CoProject (short for co-sourced
project) names and identifies a co-sourced project.</xs:documentation>
       </xs:annotation>
       </xs:element>
       <xs:element name="coSs" maxOccurs="1" minOccurs="0" type="ssRefType">
       </xs:element>
     </xs:sequence>
   </xs:choice>
 </xs:complexType>

The element coSs is optional and may occur 0 or 1 times.
Here is a snippet of the corresponding XML:

<sourcingScope>
  <relations>
    <coProject name="&gt;project &quot;full&quot; name&lt;"
short="project short name" id="PROJ_ID"/>
    <coSs number="9876-12" id="54987321"/>

    <coProject name="blubberbla" short="blubberbla short name"
id="blubber_bla"/>
   
    <coProject name="CoSourced01Vu" short="MVP 02 w/ VU"
id="PROJ_ID_01"/>
    <coSs number="9999-99" id="12345678"/>
  </relations>
</sourcingScope>


I want coProject and coSs be coupled together on the Java side in one object
in the list. The coupling is defined by the order of the elements. So the
first obj in the list would have PROJ_ID and 54987321; second only
blubber_bla without a coSs info and the third processed like the 1st one.
The thing with the lead choice is an additional case (then there are no
co-elements and exactly two lead elements).

The Java list element (which is actually a hibernate domain object) looks
like this:

public class VuRelation implements java.io.Serializable {

  private Long id; // internal id
  private SourcingScope sourcingScope; // ref to parent

  // set for lead-sourcing
  private String leadVuId;
  private String leadVuNr;

  private String leadProjectId;
  private String leadProjectShortName;
  private String leadProjectName;

  // set for co-sourcing
  private String coVuId;
  private String coVuNr;

  private String coProjectId;
  private String coProjectShortName;
  private String coProjectName;

... and linked in the main object like this:

public class SourcingScope implements java.io.Serializable {
  private Set<VuRelation> relations = new HashSet<VuRelation>();
}


How should the castor mapping look like for this case? How can I "tell"
castor to encapsulate certain elements on the same hierarchy xml level into
one object (combined with optional elements)?

Any help is appreciated!
--
Sent from the Castor - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

  http://xircles.codehaus.org/manage_email


©2008 junlu.com - Jax Systems, LLC, U.S.A.