Example usage for javax.xml.stream XMLInputFactory createFilteredReader

List of usage examples for javax.xml.stream XMLInputFactory createFilteredReader

Introduction

In this page you can find the example usage for javax.xml.stream XMLInputFactory createFilteredReader.

Prototype

public abstract XMLEventReader createFilteredReader(XMLEventReader reader, EventFilter filter)
        throws XMLStreamException;

Source Link

Document

Create a filtered event reader that wraps the filter around the event reader

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new StringReader("<a></a>"));
    reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());
    System.out.println(reader.getEventType());
}

From source file:MainClass.java

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());
}

From source file:MainClass.java

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;
        }/*from w  ww  . java 2 s  .c o  m*/
    });
    reader = inputFactory.createFilteredReader(reader, new ElementOnlyFilter());

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

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();

    XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream("input.xml"));
    InterestingElementFilter filter = new InterestingElementFilter();
    XMLEventReader interestingElementReader = xmlInputFactory.createFilteredReader(xmlEventReader, filter);

    while (interestingElementReader.hasNext()) {
        XMLEvent xmlEvent = interestingElementReader.peek();
        if (xmlEvent.isStartElement()) {
            System.out.println(xmlEvent.asStartElement().getName());
        }//www.j  av a2  s .com
        interestingElementReader.next();
    }
}

From source file:EventFilterExample.java

public static void main(String[] args) throws Exception {
    File file = new File("text.xml");

    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLEventReader reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    System.out.println("Unfiltered Count = " + countEvents(reader));

    reader = inputFactory.createXMLEventReader(new FileInputStream(file));

    EventFilter filter = new ElementOnlyFilter();
    reader = inputFactory.createFilteredReader(reader, filter);

    System.out.println("Filtered Count = " + countEvents(reader));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    XMLInputFactory inputFactory = XMLInputFactory.newInstance();
    XMLStreamReader reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));
    System.out.println(countEvents(reader));

    reader = inputFactory.createXMLStreamReader(new FileInputStream(new File("test.xml")));

    StreamFilter filter = new ElementOnlyFilter();
    reader = inputFactory.createFilteredReader(reader, filter);

    System.out.println(countEvents(reader));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    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);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new Main());

    int eventType = xmlr.getEventType();
    printEventType(eventType);/*from  w w w . ja  va 2  s .  c  o m*/

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:MyStreamFilter.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    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);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyStreamFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);//  w ww  . jav a2 s. co  m

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String filename = "yourXML.xml";

    XMLInputFactory xmlif = null;

    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);

    System.out.println("FACTORY: " + xmlif);
    System.out.println("filename = " + filename);

    FileInputStream fis = new FileInputStream(filename);

    XMLStreamReader xmlr = xmlif.createFilteredReader(xmlif.createXMLStreamReader(fis), new MyFilter());

    int eventType = xmlr.getEventType();
    printEventType(eventType);//from   ww w. j a v a  2s . c o  m

    while (xmlr.hasNext()) {
        eventType = xmlr.next();
        printEventType(eventType);
        printName(xmlr, eventType);
        printText(xmlr);

        if (xmlr.isStartElement()) {
            printAttributes(xmlr);
        }
        printPIData(xmlr);
    }
}

From source file:org.psikeds.knowledgebase.xml.impl.XMLParser.java

/**
 * Helper for parsing big XML files using JAXB in combination with StAX.<br>
 * All classes in the specified package can be parsed.<br>
 * <b>Note:</b> The XML reader will not be closed. This must be invoked by
 * the caller afterwards!<br>// ww w.ja  va 2  s .  c o m
 * 
 * @param xml
 *          Reader for XML-Data
 * @param packageName
 *          Name of the package containing the JAXB-Classes,
 *          e.g. org.psikeds.knowledgebase.jaxb
 * @param handler
 *          Callback handler used to process every single found
 *          XML-Element (@see
 *          org.psikeds.knowledgebase.xml.KBParserCallback#handleElement
 *          (java.lang.Object))
 * @param filter
 *          EventFilter used for StAX-Parsing
 * @param numSkipped
 *          Number of Elements to be skipped,
 *          e.g. numSkipped = 1 for skipping the XML-Root-Element.
 * @return Total number of unmarshalled XML-Elements
 * @throws XMLStreamException
 * @throws JAXBException
 */
public static long parseXmlElements(final Reader xml, final String packageName, final KBParserCallback handler,
        final EventFilter filter, final int numSkipped) throws XMLStreamException, JAXBException {

    // init stream reader
    final XMLInputFactory staxFactory = XMLInputFactory.newInstance();
    final XMLEventReader staxReader = staxFactory.createXMLEventReader(xml);
    final XMLEventReader filteredReader = filter == null ? staxReader
            : staxFactory.createFilteredReader(staxReader, filter);

    skipXmlElements(filteredReader, numSkipped);

    // JAXB with specific package
    final JAXBContext jaxbCtx = JAXBContext.newInstance(packageName);
    final Unmarshaller unmarshaller = jaxbCtx.createUnmarshaller();

    // parsing und unmarshalling
    long counter = 0;
    while (filteredReader.peek() != null) {
        final Object element = unmarshaller.unmarshal(staxReader);
        handleElement(handler, element);
        counter++;
    }
    return counter;
}