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

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

Element repetition

This section explains how to parse repetition of element 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:element name="list">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="int" type="local:int" 
          minOccurs="0" maxOccurs="unbounded"
          form="qualified"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Sample:

<foozy:list
 xmlns:foozy="http://www.lares.dti.ne.jp/~foozy/">
  <foozy:int>
      12345678
  </foozy:int>
  <foozy:int>
      23456789
  </foozy:int>
  <foozy:int>
      34567890
  </foozy:int>
  <foozy:int>
      45678901
  </foozy:int>
</foozy:list>

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 listElement =
    new CompositeElement(null, uri, "list");

ElementRepetition repetition =
    new ElementRepetition(listElement);

// Registrate "repetition"
// under "listElement"
listElement.addMustItem(repetition);

IntElement intElement =
    new IntElement(repetition, uri, "int");

// Set "intElement"
// as repetition target of "repetition"
repetition.setElement(intElement);

Element object(s) creation

By constructor argument, you may notice that "intElement" is not a child of "listElement" but of "repetition". "ElementRepetition" behaves as intermediate element in object structure, but as transparent element in XML document structure.

And you may also notice that there is only one IntElement instance even though you want to parse repetition of element. ElementRepetition re-uses "setElement()"ed instance for parsing each repetition. This can reduce cost to create intermediate objects.

NOTE: When you have troubles about ElementRepetition, please check (1)ElementRepetition#setElement(Element) invocation, and (2)parent of repetition target element.

Get repeated values

You will want to store repeated integer values.

One of the ways to get value on each determination in repetition is overriding of "notifyDetermined(ParseContext)" method .


final LinkedList list = new LinkedList();

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

CompositeElement listElement =
    new CompositeElement(null, uri, "list");

ElementRepetition repetition =
    new ElementRepetition(listElement);

IntElement intElement =
    new IntElement(repetition, uri, "int"){
        protected void notifyDetermined(ParseContext context){
            list.add(getInteger(false));
        }
    };

// Set "intElement"
// as repetition target of "repetition"
repetition.setElement(intElement);

Notification of value determination (1)

For detail about parameter of getInteger(), please see "Custom element", or API document.

Another way is using "Notification". Using "Notification" is recommended even though overriding approach is enough simple and easy to choose it.


final LinkedList list = new LinkedList();

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

CompositeElement listElement =
    new CompositeElement(null, uri, "list");

ElementRepetition repetition =
    new ElementRepetition(listElement);

IntElement intElement =
    new IntElement(repetition, uri, "int");

Notification notification = new Notification(){
    public void elementStarted(Element element,
                               ParseContext context,
                               Attributes attributes)
        throws SAXException
    {
        // NOP
    }

    public void elementEnded(Element element,
                             ParseContext context)
        throws SAXException
    {
        list.add(((IntElement)element).getInteger(false));
    }
};

intElement.addNotification(notification);

// Set "intElement"
// as repetition target of "repetition"
repetition.setElement(intElement);

Notification of value determination (2)

By the way, ElementRepetition fires elementEnded() of Notification at the end of repetition. So, please register Notification to the element which you want to repeat.

Parse document

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


ElementDrivenHandler handler =
    new ElementDrivenHandler(listElement);

handler.parse(reader);

Parse document

After ElementDrivenHandler#parse invocation, you can get integer values from list stored by repeated "intElement".

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


To next section "Custom element"

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 ElementRepetition
  2. ElementRepetition delegates "startElement" to Element(IntElement now)
  3. IntElement returns inself as next event receiver
  4. ElementDrivenHandler holds "intElement" as next event receiver
  5. ElementDrivenHandler invokes methods on "intElement"
  6. .....
  7. ElementDrivenHandler invokes "endElement" on IntElement
  8. IntElement invokes "notifyDetermined" on itself
  9. IntElement returns "repetition" as next event receiver
  10. ElementDrivenHandler holds "repetition" as next event receiver
  11. ElementDrivenHandler invokes methods on "repetition"

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
AbstractElement jp.ne.dti.lares.foozy.sasax.AbstractElement
CompositeElement jp.ne.dti.lares.foozy.sasax.CompositeElement
ElementDrivenHandler jp.ne.dti.lares.foozy.sasax.ElementDrivenHandler
ElementRepetition jp.ne.dti.lares.foozy.sasax.ElementRepetition
IntElement jp.ne.dti.lares.foozy.sasax.IntElement
Notification jp.ne.dti.lares.foozy.sasax.Notification
ParseContext jp.ne.dti.lares.foozy.sasax.ParseContext