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

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

Simple element

This section explains how to parse XML document with SASAX by example of parsing simple element having text.

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="int" type="local:int"/>
</xs:schema>

Sample:

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

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/";

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

Element object(s) creation

"null" argument for constructore means that "intElement" has no parent and is root one.

You can parse other data types by other classes derived from ValueElement (or NumberElement). Please see API document for detail.

Parse document

To parse XML document with "Element" implementation class(es), you should adapt it to SAXParser (of JAXP) by "ElementDrivenHandler" as shown below.

ElementDrivenHandler handler =
new ElementDrivenHandler(intElement);

// when you want to read XML document from 
// "reader" as java.io.Reader:
handler.parse(reader);

// when you want to read XML document from
// "document" as java.lang.String:
handler.parse(document);

Parse document

Both ElementDrivenHandler#parse methods get SAXParser from SAXParserFactory, then invoke SAXParser#parse with themselves internally. This causes invocation of methods defined in Element corresponded to appropriate SAX events.

NOTE: In this tutorial, memory/performance efficiency is ignored for simple explanation. Please see API document for efficient use.

After ElementDrivenHandler#parse invocation, you can get integer value from "intElement" by getInteger method of IntElement. 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.SimpleDemo under src/demo/sasax/src in distribution. This class is included in demo.jar of binary distribution.


To next section "Composite 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 SAXParser, ElementDrivenHandler and Element implementation class is shown as below:

  1. SAXParser invokes "startElement" on ElementDrivenHandler
  2. ElementDrivenHandler invokes "startElement" on Element held currently
  3. Element(IntElement now) returns itself as next event receiver
  4. ElementDrivenHandler holds "intElement" as next event receiver
  5. .....
  6. SAXParser invokes "endElement" on ElementDrivenHandler
  7. ElementDrivenHandler invokes "startElement" on Element held currently
  8. Element(IntElement now) returns null as next event receiver
  9. ElementDrivenHandler holds "null" as next event receiver

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
Element jp.ne.dti.lares.foozy.sasax.Element
ElementDrivenHandler jp.ne.dti.lares.foozy.sasax.ElementDrivenHandler
IntElement jp.ne.dti.lares.foozy.sasax.IntElement
NumberElement jp.ne.dti.lares.foozy.sasax.NumberElement
ValueElement jp.ne.dti.lares.foozy.sasax.ValueElement