Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.FileInputStream;

import javax.xml.stream.EventFilter;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

public class Main {
    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());
            }
            interestingElementReader.next();
        }
    }
}

class InterestingElementFilter implements EventFilter {
    public boolean accept(XMLEvent event) {
        if (event.isStartElement()) {
            StartElement startElement = event.asStartElement();
            String name = startElement.getName().getLocalPart();
            if (name.equals("InterestingElement")) {
                return true;
            }
        }
        return false;
    }
}