background image

Creating the Filter

<< Building and Running the Event Example | Filtering the Stream >>
<< Building and Running the Event Example | Filtering the Stream >>

Creating the Filter

XMLInputFactory xmlif = null ;
try {
xmlif = XMLInputFactory.newInstance();
xmlif.setProperty(
XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
Boolean.TRUE);
xmlif.setProperty(
XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
Boolean.FALSE);
xmlif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE,
Boolean.TRUE);
xmlif.setProperty(XMLInputFactory.IS_COALESCING,
Boolean.TRUE);
} catch (Exception ex) {
ex.printStackTrace();
}
System.out.println(
"FACTORY: " + xmlif);
System.out.println(
"filename = "+ filename);
Creating the Filter
The next step is to instantiate a file input stream and create the stream filter:
FileInputStream fis = new FileInputStream(filename);
XMLStreamReader xmlr = xmlif.createFilteredReader(
xmlif.createXMLStreamReader(fis), new MyStreamFilter());
int eventType = xmlr.getEventType();
printEventType(eventType);
while(xmlr.hasNext()) {
eventType = xmlr.next();
printEventType(eventType);
printName(xmlr,eventType);
printText(xmlr);
if (xmlr.isStartElement()) {
printAttributes(xmlr);
}
printPIData(xmlr);
System.out.println(
"-----------------------------");
}
Capturing the Event Stream
The next step is to capture the event stream. This is done in basically the same way as in the
Event example.
Example Code
The Java EE 5 Tutorial · September 2007
576