XMLInputFactory: createFilteredReader(XMLEventReader reader, EventFilter filter) : XMLInputFactory « javax.xml.stream « Java by API





XMLInputFactory: createFilteredReader(XMLEventReader reader, EventFilter filter)

  

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

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

public class Main {
  public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(new File("a.xml")));
    reader = inputFactory.createFilteredReader(reader, new EventFilter() {
      public boolean accept(XMLEvent event) {
        return false;
      }
    });
    reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());

    System.out.println(reader.hasNext());
    System.out.println(reader.next());
  }
}

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

1.XMLInputFactory.IS_COALESCING
2.XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES
3.XMLInputFactory.setProperty(String name, Object value)
4.XMLInputFactory: createFilteredReader(XMLStreamReader reader, StreamFilter filter)
5.XMLInputFactory: createXMLEventReader(InputStream stream)
6.XMLInputFactory: createXMLEventReader(Reader reader)
7.XMLInputFactory: createXMLStreamReader(InputStream stream)
8.XMLInputFactory: setXMLResolver(XMLResolver resolver)