Stax XMLStreamReader check for the next event without moving ahead

“Going back” in a stream implies some kind of memory, so there is no point in sticking to the most memory-efficient tool. XMLEventReader can handle this with ease: public class Main { public static void main(String args[]) throws Exception { Unmarshaller aUnmarshaller = JAXBContext.newInstance(A.class).createUnmarshaller(); Unmarshaller bUnmarshaller = JAXBContext.newInstance(B.class).createUnmarshaller(); Unmarshaller cUnmarshaller = JAXBContext.newInstance(C.class).createUnmarshaller(); try (InputStream input … Read more