Inline Stream Filter : StreamFilter « XML « Java






Inline Stream Filter

 

import java.io.File;
import java.io.FileInputStream;

import javax.xml.stream.EventFilter;
import javax.xml.stream.StreamFilter;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.events.XMLEvent;

public class MainClass {
  public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("l.xml")));
    reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());
    System.out.println(reader.getEventType());
  }
}

class ElementOnlyFilter implements EventFilter, StreamFilter {
  public boolean accept(XMLEvent event) {
    return accept(event.getEventType());
  }

  public boolean accept(XMLStreamReader reader) {
    return accept(reader.getEventType());
  }

  private boolean accept(int eventType) {
    return eventType == XMLStreamConstants.START_ELEMENT
        || eventType == XMLStreamConstants.END_ELEMENT;
  }
}

 








Related examples in the same category