Home of: [Atelier "FUJIGURUMA"] >> [SASAX hosted by SourceForge.net]

SEE "For Readers of English Version",
or Japanese version of this page

Composite element

This section explains how to parse composite element consisting of other elements with SASAX.

Define document structure

Schema in XML Schema and sample of document is shown in "Document structure".

Schema in XML Schema:

<xs:schema 
  targetNamespace="http://www.lares.dti.ne.jp/~foozy/"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:local="http://www.lares.dti.ne.jp/~foozy/">

  <xs:simpleType name="int">
    <xs:restriction base="xs:integer"/>
  </xs:simpleType>

  <xs:simpleType name="string">
    <xs:restriction base="xs:token"/>
  </xs:simpleType>

  <xs:element name="composite">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="int" type="local:int"
          form="qualified"/>
        <xs:element name="string" type="local:string"
          minOccurs="0"
          form="qualified"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Sample:

<foozy:composite
 xmlns:foozy="http://www.lares.dti.ne.jp/~foozy/">
  <foozy:int>
    12245678
  </foozy:int>
  <foozy:string>
    optional text message
  </foozy:string>
</foozy:composite>

Document structure

You can know below things about target document.

Create element object(s)

Objects to parse the document described in the former are created as shown in "Element object(s) creation".


String uri = "http://www.lares.dti.ne.jp/~foozy/";

CompositeElement compositeElement =
new CompositeElement(null, uri, "composite");

IntElement intElement =
new IntElement(compositeElement, uri, "int");
composite.addMustItem(intElement);

StringElement stringElement =
new StringElement(compositeElement, uri, "string");
composite.addOptionalItem(stringElement);

Element object(s) creation

After creation of elements belonging to "composite", they are registered to "composite". The difference of registration method (addMustItem and addOptionalItem) is the difference of element necessity.

NOTE: Many of unexpected behaviors around CompositeElement are caused by missing invocation of addMustItem (or addOptionalItem).

Please check whether they are invoked or not.

Parse document

You can parse XML document by the code shown in "Parse document" as you know.


ElementDrivenHandler handler =
new ElementDrivenHandler(compositeElement);
handler.parse(reader);

Parse document

After ElementDrivenHandler#parse invocation, you can (may) get integer (string) value from "intElement" ("stringElement") by getInteger method of IntElement (getString of StringElement). For detail about parameter of that method, please see "Custom element", or API document.

Complete code for this tutorial section is jp.ne.dti.lares.foozy.sasax.CompositeDemo under src/demo/sasax/src in distribution. This class is included in demo.jar of binary distribution.


To next section "Hook of parsing"

Detailed information

Class diagram

Class diagram in this section is shown below:

Class diagram
Class diagram (click for large figure)

Object diagram

Object diagram in this section is shown below:

Object diagram
Object diagram (click for large figure)

Sequence diagram

Processing flow between ElementDrivenHandler, CompositeElement and Element implementation class is shown as below:

  1. ElementDrivenHandler invokes "startElement" on CompositeElement
  2. CompositeElement returns itself as next event receiver
  3. ElementDrivenHandler invokes "startElement" on CompositeElement again
  4. CompositeElement delegates "startElement" to Element(IntElement now)
  5. IntElement returns inself as next event receiver
  6. ElementDrivenHandler holds "intElement" as next event receiver
  7. ElementDrivenHandler invokes methods on "intElement"
  8. .....
  9. ElementDrivenHandler invokes "endElement" on IntElement
  10. IntElement returns "compositeElement" as next event receiver
  11. ElementDrivenHandler holds "compositeElement" as next event receiver
  12. ElementDrivenHandler invokes methods on "compositeElement"

And sequence diagram is shown below.

Sequence diagram
Sequence diagram (click for large figure)

Class names

In this tutorial, abbreviated class names are used. Complete names are shown below.

NotationFull name
CompositeElement jp.ne.dti.lares.foozy.sasax.CompositeElement
ElementDrivenHandler jp.ne.dti.lares.foozy.sasax.ElementDrivenHandler
IntElement jp.ne.dti.lares.foozy.sasax.IntElement
StringElement jp.ne.dti.lares.foozy.sasax.StringElement