Site Navigation:

sbxb

A complex binding example

This sample's code is located in the package {@link org.otvl.sbxbsamp.complex}.

The SoftBean object model

The sample's object model is provided here:

This sample uses the classic SoftBean's patterns for simple, indexed and mapped properties.

The target XML format

Here is a sample of a XML document to be mapped to the collection of SoftBean objects.

<A a1="xa1">

<a2>xa2</a2>

<a3 a4="xa4">xa3</a3>

<x>

<a5 a6="xa6">xa5</a5>

<B b1="xb1">xBa

<b2>xb2</b2>xBb

</B>

</x>

<xB b1="xxb1">xxBa

<b2>xxb2</b2>xxBb

</xB>

<xB b1="xyb1">xyBa

<b2>xyb2</b2>xyBb

</xB>

<c c1="xc1">

<c2>xc2</c2>

<y>

<c3 c4="xc4">xc3</c3>

<yB b1="yxb1">yxBa

<b2>yxb2</b2>yxBb

</yB>

<yB b1="yyb1">yyBa

<b2>yyb2</b2>yyBb

</yB>

</y>

<c5>xc5</c5>

</c>

<a7>xa7</a7>

<xD d1="typeE" d2="xxd2">xxD

<e1>xe1</e1>

</xD>

<xD d1="typeF" d2="xyd2">xyD

<f1>xf1</f1>

</xD>

<xD d1="typeU" d2="xzd2">xzD

</xD>

<G>

<att1>xatt1</att1>

</G>

<G>

<att2>xatt2</att2>

</G>

<subA>

<att8>xatt8</att8>

</subA>

<subA>

<att9>xatt9</att9>

</subA>

</A>

The binding specification

The binding specification can be written as following:

mapx.org.otvl.sbxbsamp.complex.ClsA.0.att1=@a1

mapx.org.otvl.sbxbsamp.complex.ClsA.1.att2=a2

mapx.org.otvl.sbxbsamp.complex.ClsA.2.att3=a3

mapx.org.otvl.sbxbsamp.complex.ClsA.3.att4=__ser[2]a3@a4

mapx.org.otvl.sbxbsamp.complex.ClsA.4.__sed=x

mapx.org.otvl.sbxbsamp.complex.ClsA.5.att5=__ser[4]x/a5

mapx.org.otvl.sbxbsamp.complex.ClsA.6.att6=__ser[5]x/a5@a6

mapx.org.otvl.sbxbsamp.complex.ClsA.7.roleB=__ser[4]x/B(mapx)

mapx.org.otvl.sbxbsamp.complex.ClsA.8.roleXB=xB(mapx)

mapx.org.otvl.sbxbsamp.complex.ClsA.9.roleC=c(mapx,attC1)

mapx.org.otvl.sbxbsamp.complex.ClsA.10.att7=a7

mapx.org.otvl.sbxbsamp.complex.ClsA.11.roleD=xD(mapx)\

{(org.otvl.sbxbsamp.complex.ClsD)(org.otvl.sbxbsamp.complex.ClsE,@d1,typeE)(org.otvl.sbxbsamp.complex.ClsF,@d1,typeF)}

mapx.org.otvl.sbxbsamp.complex.ClsA.12.roleG=G(mapx1)

mapx.org.otvl.sbxbsamp.complex.ClsA.13.roleG=G(mapx2)

mapx.org.otvl.sbxbsamp.complex.ClsA.14.att8=subA/att8

mapx.org.otvl.sbxbsamp.complex.ClsA.15.att9=subA/att9

mapx.org.otvl.sbxbsamp.complex.ClsB.0.b1=@b1

mapx.org.otvl.sbxbsamp.complex.ClsB.1.b=.

mapx.org.otvl.sbxbsamp.complex.ClsB.2.b2=b2

mapx.org.otvl.sbxbsamp.complex.ClsC.0.attC1=@c1

mapx.org.otvl.sbxbsamp.complex.ClsC.1.attC2=c2

mapx.org.otvl.sbxbsamp.complex.ClsC.2.__sed=y

mapx.org.otvl.sbxbsamp.complex.ClsC.3.attC3=__ser[2]y/c3

mapx.org.otvl.sbxbsamp.complex.ClsC.4.attC4=__ser[3]y/c3@c4

mapx.org.otvl.sbxbsamp.complex.ClsC.5.roleYB=__ser[2]y/yB(mapx)

mapx.org.otvl.sbxbsamp.complex.ClsC.6.attC5=c5

mapx.org.otvl.sbxbsamp.complex.ClsD.0.attD1=@d1

mapx.org.otvl.sbxbsamp.complex.ClsD.1.attD2=@d2

mapx.org.otvl.sbxbsamp.complex.ClsD.2.attD=.

mapx.org.otvl.sbxbsamp.complex.ClsE.0.attD1=@d1

mapx.org.otvl.sbxbsamp.complex.ClsE.1.attD2=@d2

mapx.org.otvl.sbxbsamp.complex.ClsE.2.attE=.

mapx.org.otvl.sbxbsamp.complex.ClsE.3.attE1=e1

mapx.org.otvl.sbxbsamp.complex.ClsF.0.attD1=@d1

mapx.org.otvl.sbxbsamp.complex.ClsF.1.attD2=@d2

mapx.org.otvl.sbxbsamp.complex.ClsF.2.attD=.

mapx.org.otvl.sbxbsamp.complex.ClsF.3.attF1=f1

mapx1.org.otvl.sbxbsamp.complex.ClsG.0.att1=att1

mapx2.org.otvl.sbxbsamp.complex.ClsG.0.att2=att2

Specialized rules for writing a binding specification

In the previous sample you can see that it is sometimes necessary to add and additional XML level, which is not directly linked to an object's element. To handle this case, we use "start element references" with the syntax "__ser[index]", where index references the rule where the element referenced has been defined. It is also possible to create any "start element" you want with the syntax "__sed" for "start element definition". This element may be later referenced like any other "start element" with the "__ser[index]" notation.

Indexed simple properties

It is also possible to handle properties implemented with arrays of simple values as XML sub-elements such as int[] or String[]. Just declare them in the binding specification as you would for simple properties, the corresponding XML sub-element will be rendered for each item in the array. When parsing the document, the indexed property will be built with one item for each bound sub-element. It should be generalized for collections of simple values, but it is not the case currently.