MAP | SASAX Documents > Parsing with SASAX > Simple element | << | >> |
This section explains how to parse XML document with SASAX by example of parsing simple element having text.
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>
You can know below things about target document.
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");
"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.
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);
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.
Class diagram in this section is shown below:
Object diagram in this section is shown below:
Processing flow between SAXParser, ElementDrivenHandler and Element implementation class is shown as below:
And sequence diagram is shown below.
In this tutorial, abbreviated class names are used. Complete names are shown below.
Notation | Full 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 |
MAP | SASAX Documents > Parsing with SASAX > Simple element | << | >> |