MAP | SASAX Documents > Parsing with SASAX > Element choice | << | >> |
This section explains how to parse runtime determined element in XML document with SASAX.
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="envelope"> <xs:complexType> <xs:choice> <xs:element name="int" type="local:int" form="qualified"/> <xs:element name="string" type="local:string" form="qualified"/> </xs:choice> </xs:complexType> </xs:element> </xs:schema> Sample - "int" case: <foozy:envelope xmlns:foozy="http://www.lares.dti.ne.jp/~foozy/"> <foozy:int> 12245678 </foozy:int> </foozy:envelope> Sample - "string" case: <foozy:envelope xmlns:foozy="http://www.lares.dti.ne.jp/~foozy/"> <foozy:string> text message </foozy:string> </foozy:envelope>
You can know below things about target document.
To handle element(s) choosen at runtime, you should define factory class for them as shown in "Define factory class".
public class IntElementFactory implements ElementFactory // { final static private String URI = "http://www.lares.dti.ne.jp/~foozy/"; final static private String NAME = "int"; final static private NameChecker CHECKER = new NameChecker(URI, NAME); //////////////////////////////////////// // Implementation of interface ElementFactory public boolean accepts(ParseContext context, String uri, String lName) { return CHECKER.accepts(uri, lName); } public Element create(Element parent){ return new IntElement(parent, URI, NAME); } }
"IntElementFactory
" examines
whether specified name is acceptable
(in "accepts()
" method).
"NameChecker
" is utility class
to examine equality of name.
And factory class creates element object under specified parent
(in "create()
" method).
Factory for "string" element is also defined as same as
IntElementFactory
.
Objects to parse the document described in the former are created as shown in "Element object(s) creation".
CompositeElement envelopeElement = new CompositeElement(null, uri, "envelope"); ElementChoice choice = new ElementChoice(envelopeElement); envelopeElement.addMustItem(choice);
But ElementChoice
is not usefull by itself.
You must register factories as shown
"Registration of factories".
choice.add(new IntElementFactory()); choice.add(new StringElementFactory());
Now specified factories are registered as one for candiate element.
You can parse XML document by the code shown in "Parse document".
ElementDrivenHandler handler = new ElementDrivenHandler(envelopeElement); handler.parse(reader); Element determined = choice.getDetermined();
After parsing XML document,
you can get determined element by
getDetermined()
method of ElementChoice
.
You can idenitfy what element is determined
both
by invoke accepts()
method on gotten element,
and by "instanceof" examination.
But using accepts()
method is recommended
for performance efficiency.
Complete code for this tutorial section is
jp.ne.dti.lares.foozy.sasax.ChoiceDemo
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 ElementDrivenHandler, ElementChoice, ElementFactory implementation class and Element created by ElementFactory 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 |
---|---|
CompositeElement | jp.ne.dti.lares.foozy.sasax.CompositeElement |
Element | jp.ne.dti.lares.foozy.sasax.Element |
ElementChoice | jp.ne.dti.lares.foozy.sasax.ElementChoice |
ElementDrivenHandler | jp.ne.dti.lares.foozy.sasax.ElementDrivenHandler |
ElementFactory | jp.ne.dti.lares.foozy.sasax.ElementFactory |
NameChecker | jp.ne.dti.lares.foozy.sasax.NameChecker |
ParseContext | jp.ne.dti.lares.foozy.sasax.ParseContext |
IntElement | jp.ne.dti.lares.foozy.sasax.IntElement |
MAP | SASAX Documents > Parsing with SASAX > Element choice | << | >> |