MAP | SASAX Documents > Parsing with SASAX > Hook of parsing | << | >> |
This section explains some utilities to parse XML document with SASAX.
Topics of this section are:
"AbstractElement
", which is the base class of almost all
"Elemenet
" implementation classes,
has "notifyDetermined(ParseContext)
" method
which is invoked when the content of element is determined.
For example,
you can extend behavior of the "composite
" element
by overriding notifyDetermined(ParseContext)
as shown below.
CompositeElement compositeElement =
new CompositeElement(null, uri, "composite"){
protected void notifyDetermined(ParseContext context){
// processing on '</composite>' is placed here
}
};
SASAX provides "Notification
" interface
to receive notification of element start/end.
public interface Notification { public void elementStarted(Element element, ParseContext context, Attributes attributes) throws SAXException; public void elementEnded(Element element, ParseContext context) throws SAXException; }
AbstractElement
has "addNotification()
" method to
register specified one into event receiver list.
Registered ones are invoked at corresponded element start/end.
For example,
you can receive start/end of "composite
" element
by below code:
compositeElement.addNotification(new Notification(){ public void elementStarted(Element element, ParseContext context, Attributes attributes) throws SAXException { // processing on '<composite>' is placed here } public void elementEnded(Element element, ParseContext context) throws SAXException { // processing on '</composite>' is placed here } });
elementEnded()
is invoked as soon as
invocation of notifyDetermined(ParseContext)
,
so almost all implementations in it can be placed in
elementEnded()
of Notification
.
ATTENTION: feature explained in this sub section is available since SASAX 1.5.
Somtimes attribute values described on start tag in XML document have
important role.
But you should keep attribute values,
if you want to use it after the start of the element
(e.g.: in elementEnded()
of Notification
,
explained above).
AbstractElement
assists your code
to access attribute values after the start of the element
by watchAttribute()
and
getAttribute()
.
You can have AbstractElement
watch and keep attribute values
by watchAttribute()
. For example:
compositeElement.watchAttribute("", "foo");
Now, compositeElement
keeps attribute value of
"foo
"(without namespace)
if it is described on the start tag in XML document.
You can get attribute value of "foo
",
which is described on start tag in XML document,
by getAttribute()
method.
compositeElement.getAttribute("", "foo");
Attribute value is kept until clear()
invocation,
which is invoked at the (re)start of parsing by such element.
Without watchAttribute()
,
you can not get value of attribute,
even though such attribute is described on start tag in XML document.
value on start tag | watching | |
---|---|---|
watched | not watched | |
described | described value | null |
not described | null |
null |
In this tutorial, abbreviated class names are used. Complete names are shown below.
Notation | Full name |
---|---|
AbstractElement | jp.ne.dti.lares.foozy.sasax.AbstractElement |
Element | jp.ne.dti.lares.foozy.sasax.Element |
Notification | jp.ne.dti.lares.foozy.sasax.Notification |
ParseContext | jp.ne.dti.lares.foozy.sasax.ParseContext |
MAP | SASAX Documents > Parsing with SASAX > Notification of element start/end | << | >> |