MAP | SASAX Documents > Parsing with SASAX > Path handling | << | >> |
This section explains how to handle XML document 'path' with SASAX.
SASAX provides the functionality to know the current hierarchy in the XML document as "path". In SASAX, "path" is the list of the "element", which contains (1)URI, (2)name and (3)attributes.
"Path" and "element" are defined
as "Path
" and "Path.Element
".
Path
provides
utilities to compare itself with another instance:
ancestor-ness, descendant-ness, and so on, for example.
Recording current hierarchy in XML document as "path" is not so expensive in performance, but is just overhead if you do not use such information in parsing.
So,
SASAX defines such, "path" information handling for example,
functionalities as "extension",
and provides the toggle to turn each extensions on/off individualy
to reduce running cost of parsing.
"path extension" is defined as "PathExtension
".
At first,
you should invoke register()
of
PathExtension
to make SASAX recognize "path extension" as "extension",
onece per JVM process.
Then, you can enable/disable "path extension" by
enable()
of
PathExtension
per parsing
(per ElementDrivenHandler
, in fact).
PathExtension.register(); ElementDrivenHandler handler = new ElementDrivenHandler(rootElement); PathExtension.enable(handler); handler.parse(document);
For example of path handling,
the code to dump current path hierarchy
on each "startElement" is shown below.
This code is part of PathDemo
placed under src/demo/sasax/src
in src/bin distribution.
This class is included in demo.jar
of binary distribution.
public Element startElement(ParseContext context, String uri, String lName, Attributes attrs) throws SAXException // { // dump current path hierarchy Path path = PathExtension.getPath(context); Iterator elements = path.getElements(); while(elements.hasNext()){ Path.Element element = (Path.Element)(elements.next()); System.out.print("/"); System.out.print(element.getLName()); } System.out.println(""); return this; }
You can parse XML document with path handling by the code shown in "Parse document" as you know.
PathExtension.register(); PathDemo rootElement = new PathDemo(null); ElementDrivenHandler handler = new ElementDrivenHandler(rootElement); PathExtension.enable(handler); handler.parse(reader);
Class diagram in this section 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 |
ParseContext | jp.ne.dti.lares.foozy.sasax.ParseContext |
ParseContextImpl | jp.ne.dti.lares.foozy.sasax.ParseContextImpl |
Path | jp.ne.dti.lares.foozy.sasax.Path |
PathExtension | jp.ne.dti.lares.foozy.sasax.PathExtension |
MAP | SASAX Documents > Parsing with SASAX > Path handling | << | >> |