Java tutorial
//package com.java2s; import javax.xml.stream.XMLEventReader; import javax.xml.stream.XMLStreamException; import javax.xml.stream.events.XMLEvent; public class Main { public static void skipWhitespaceAndComments(XMLEventReader xml) { XMLEvent peek = peek(xml); while (isWhitespace(peek) || isComment(peek)) { nextEvent(xml); peek = peek(xml); } } /** * Calls xml.peek() and unchecks the exception. * @param xml * @return */ public static XMLEvent peek(XMLEventReader xml) { try { return xml.peek(); } catch (XMLStreamException e) { throw new RuntimeException(e); } } public static boolean isWhitespace(XMLEvent e) { if (e.isCharacters() && e.asCharacters().isWhiteSpace()) { return true; } return false; } public static boolean isComment(XMLEvent e) { if (e.getEventType() == XMLEvent.COMMENT) { return true; } return false; } /** * Calls xml.nextEvent() and unchecks the exception. * * @param xml * @return xml.nextEvent(), or throws a RuntimeException. */ public static XMLEvent nextEvent(XMLEventReader xml) { try { return xml.nextEvent(); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }