Creating an Input Factory
FILTER
S
AMPLE
M
Y
S
TREAM
F
ILTER
.
JAVA
109
filter Sample MyStreamFilter.java
Located in the
<javaee.tutorial.home>/examples/stax/filter
directory,
MyStreamFilter.java
demonstrates how to use the StAX stream filter API to
filter out events not needed by your application. In this example, the parser filters
out all events except
StartElement
and
EndElement
.
Implementing the StreamFilter Class
The MyStreamFilter implements
javax.xml.stream.StreamFilter
:
public class MyStreamFilter implements
javax.xml.stream.StreamFilter{
Creating an Input Factory
The next step is to create an instance of XMLInputFactory. In this case, various
properties are also set on the factory:
XMLInputFactory xmlif = null ;
try{
xmlif = XMLInputFactory.newInstance();
xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENC
ES,Boolean.TRUE);
xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTIT
IES,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);