Contents | Package | Class | Tree | Deprecated | Index | Help | XML for Java 1.0.4 | ||
PREV | NEXT | SHOW LISTS | HIDE LISTS |
This interface is implemented by filter programs which do not want to deal with the complexities of manipulating an entire XML document hierarchy. Rather, filter programs only want to recieve control when a pre-registered set of tags are recognized by the parser.
For example, a filter program may want to convert all <email>foo@bar</email>
tags to <url href="mailto:foo@bar"/>
. In this case, the email
tag is registered with the XML4J parser instance, prior to parsing the input stream, by using
the addElementHandler
method. Subsequently, the implemented handleElement
method will be invoked by the XML4J parser instance when the input stream is read; the
implemented handleElement
method is responsible for manipulating the child
objects of the <email>
element. This logic is implemented in the
following code fragment:
public class EMAILFilter implements ElementHandler {
public TXElement handleElement(TXElement originalElement) {
String addr;
if (0 != (addr = originalElement.getText()).length()) {
TXElement newElement = new TXElement("URL");
newElement.setAttribute("HREF", "mailto:"+addr);
originalElement = newElement;
}
return originalElement;
}
public static void main(String[] argv) {
...
Parser p = new Parser(fname);
p.addElementHandler(new EMAILFilter(), "EMAIL"); // Watch for EMAIL tag
// Note: If the above line is changed to p.addElementHandler(new EMAILFilter());
,
// EMAILFilter is invoked when EACH end tag is recognized by the parser.
TXDocument doc = p.readStream(p.getInputStream(fname, null, fname));
// doc instance now contains updated EMAIL Elements.
...
}
Method Summary | |
TXElement | handleElement(TXElement element)
|
Method Detail |
public TXElement handleElement(TXElement element)
element
- An Element that matches the specified name on a prior call to
parser.addElementHandler
. Note that namespace names
may also be included in Element names.
Contents | Package | Class | Tree | Deprecated | Index | Help | |||
PREV | NEXT | SHOW LISTS | HIDE LISTS |