Example:
ElementScanner f = new ElementScanner(); // All descendants of x named y f.addElementListener(new MyImpl(), "x//y"); // All grandchilden of y named t f.addElementListener(new MyImpl(), "y/* /t"); ElementListener l2 = new MyImpl2(); f.addElementListener(l2, "/*"); // Root element f.addElementListener(l2, "z"); // Any node named z ElementListener l3 = new MyImpl3(); // Any node having an attribute "name" whose value contains ".1" f.addElementListener(l3, "*[contains(@name,'.1')]"); // Any node named y having at least one "y" descendant f.addElementListener(l3, "y[.//y]"); f.parse(new InputSource("test.xml"));
The XPath interpreter can be changed (see {@link XPathMatcher}). The default implementation is a mix of the Jakarta RegExp package and the Jaxen XPath interpreter.
ElementScanner splits XPath expressions in 2 parts: a node selection pattern and an optional test expression (the part of the XPath between square backets that follow the node selection pattern).
Regular expressions are used to match nodes applying the node selection pattern. This allows matching node without requiring to build them (as Jaxen does).
If a test expression appears in an XPath expression, Jaxen is used to match the built elements against it and filter out those not matching the test.
As a consequence of using regular expressions, the or" operator ("|
" in XPath) is not supported in node selection patterns but can be achieved by registering the same listener several times with different node patterns.
Note: The methods marked with "[ContentHandler interface support]" below shall not be invoked by the application. Their usage is reserved to the XML parser.
@author Laurent Bihanic
|
|