MAP | SASAX Documents > Parsing with SASAX > Element repetition | << | >> |
This section explains how to parse repetition of element 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: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>
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/" 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);
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.
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);
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);
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.
You can parse XML document by the code shown in "Parse document" as you know.
ElementDrivenHandler handler = new ElementDrivenHandler(listElement); handler.parse(reader);
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.
Class diagram in this section is shown below:
Object diagram in this section is shown below:
Processing flow between ElementDrivenHandler, CompositeElement 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 |
---|---|
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 |
MAP | SASAX Documents > Parsing with SASAX > Element repetition | << | >> |