background image

Filtering the Stream

<< Creating the Filter | Building and Running the Filter Example >>
<< Creating the Filter | Building and Running the Filter Example >>

Filtering the Stream

Filtering the Stream
The final step is to filter the stream:
public boolean accept(XMLStreamReader reader) {
if (!reader.isStartElement() && !reader.isEndElement())
return false;
else
return true;
}
Returning the Output
When you run the Filter example, the MyStreamFilter class is compiled, and the XML stream
is parsed as events and returned to STDOUT. For example, an Author event is returned as follows:
EVENT TYPE(1):START_ELEMENT
HAS NAME: Author
HAS NO TEXT
HAS NO ATTRIBUTES
-----------------------------
EVENT TYPE(2):END_ELEMENT
HAS NAME: Author
HAS NO TEXT
-----------------------------
Similarly, a Cost event is returned as follows:
EVENT TYPE(1):START_ELEMENT
HAS NAME: Cost
HAS NO TEXT
HAS ATTRIBUTES:
ATTRIBUTE-PREFIX:
ATTRIBUTE-NAMESP: null
ATTRIBUTE-NAME:
currency
ATTRIBUTE-VALUE: USD
ATTRIBUTE-TYPE:
CDATA
-----------------------------
EVENT TYPE(2):END_ELEMENT
HAS NAME: Cost
HAS NO TEXT
-----------------------------
See
"Iterator API" on page 553
and
"Reading XML Streams" on page 561
for a more detailed
discussion of StAX event parsing.
Example Code
Chapter 18 · Streaming API for XML
577