List of usage examples for javax.xml.stream XMLEventReader peek
public XMLEvent peek() throws XMLStreamException;
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()); }/* w w w.j a va2s .c o m*/ interestingElementReader.next(); } }
From source file:Main.java
public static void main(String[] args) throws Exception { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLEventReader xmlr = xif.createXMLEventReader((new FileInputStream(new File("./file.xml")))); boolean inline = false; StringBuffer sb = new StringBuffer(); while (xmlr.hasNext()) { XMLEvent event = xmlr.nextEvent(); if (event.isStartElement()) { StartElement element = (StartElement) event; if ("data".equals(element.getName().toString().trim())) { inline = true;/* w w w . j a va 2s. c om*/ } } if (inline) { sb.append(xmlr.peek()); } if (event.isEndElement()) { EndElement element = (EndElement) event; if ("data".equals(element.getName().toString().trim())) { inline = false; System.out.println(sb.toString()); sb.setLength(0); } } } }
From source file:Main.java
/** * Calls xml.peek() and unchecks the exception. * @param xml//from ww w .j a v a 2 s. c om * @return */ public static XMLEvent peek(XMLEventReader xml) { try { return xml.peek(); } catch (XMLStreamException e) { throw new RuntimeException(e); } }
From source file:Main.java
/** * Advances the event stream until it encounters a start or end tag, but * does not actaully read the event./*from w w w . j av a2 s.c om*/ * * @param reader The reader to peek. * @return The next StartElement or EndElement event, retrieved using * <code>peek()</code>, or <code>null</code> if the end of the * stream was encountered before any tag event. * @throws XMLStreamException If an error occurs reading the stream. */ public static final XMLEvent nextTag(XMLEventReader reader) throws XMLStreamException { while (reader.hasNext()) { XMLEvent nextEvent = reader.peek(); if (nextEvent.isStartElement() || nextEvent.isEndElement()) { return nextEvent; } else { // eat the event. reader.nextEvent(); } } return null; }
From source file:Main.java
public static boolean nextTagIsEndOf(String name, XMLEventReader xml) throws XMLStreamException { skipWhitespaceAndComments(xml);/* ww w. j av a2 s. c om*/ return isEndTag(xml.peek(), name); }
From source file:Main.java
/** * Reads the events from the provided event stream until either a start or * end tag is encountered. In the former case, the start tag will be * returned if it matches the specified QName, but if it doesn't match, an * end tag is encountered, or the stream ends, <code>null</code> will be * returned. After returning, the stream will be positioned just before the * start element. The start element will not be consumed by this method. * /* w w w . java2 s . co m*/ * @param reader The event stream from which to read. * @param name The name of the element to read, or <code>null</code> to * read any start tag. * @return The StartElement read from the stream, or <code>null</code> if * the encountered start tag didn't match the specified QName, an * end tag was found first, or the stream ended before a start * element was found. * @throws XMLStreamException If an error occurs reading the stream. */ public static final StartElement nextElement(XMLEventReader reader, QName name) throws XMLStreamException { while (reader.hasNext()) { XMLEvent nextEvent = reader.peek(); if (nextEvent.isStartElement()) { StartElement start = nextEvent.asStartElement(); if (name == null || start.getName().equals(name)) { return start; } else { break; } } else if (nextEvent.isEndElement()) { break; } else { // consume the event. reader.nextEvent(); } } return null; }
From source file:Main.java
/** * Utility method that throws an exception if the provided reader is not * positioned before a StartElement event with the specified tag name. * // w w w . ja va 2 s . c o m * @param reader The reader to test. * @param qname The required name of the start-tag. If <code>null</code>, * any start tag is accepted. * @throws XMLStreamException If an error occurs reading from the stream. */ public static final void requireStartElement(XMLEventReader reader, QName qname) throws XMLStreamException { if (reader.hasNext()) { XMLEvent nextEvent = reader.peek(); if (nextEvent.isStartElement()) { if (qname != null) { StartElement start = nextEvent.asStartElement(); QName name = start.getName(); if (!name.equals(qname)) { throw new XMLStreamException( "Encountered unexpected element; expected " + qname + ", but found " + name); } } } else { throw new XMLStreamException("Encountered unexpected event; expected " + qname + " start-tag, but found event " + nextEvent); } } else { throw new XMLStreamException("Encountered unexpected end of stream; expected element " + qname); } }
From source file:Main.java
/** * Copies an element and all its content from the provided event reader, to * the provided event consumer. The event reader must be positioned before a * start element event, or this method has no effect. * /*from www. ja v a2s .c o m*/ * @param reader The reader from which to read the events. * @param consumer The destination for read events, or <code>null</code> to * ignore all events. * @throws XMLStreamException If an error occurs reading or writing the * events. */ public static final void copyElement(XMLEventReader reader, XMLEventConsumer consumer) throws XMLStreamException { if (!reader.hasNext()) return; XMLEvent event = reader.peek(); if (!event.isStartElement()) return; int depth = 0; do { XMLEvent currEvt = reader.nextEvent(); if (currEvt.isStartElement()) { depth++; } else if (currEvt.isEndElement()) { depth--; } if (consumer != null) { consumer.add(currEvt); } } while (depth > 0 && reader.hasNext()); }
From source file:Main.java
/** * Copies all events within a <code>StartElement</code> until the matching * <code>EndElement</code> is reached. This method assumes that the reader * is positioned after the <code>StartElement</code> event, and when the * method completes, the stream will be positioned before the * <code>EndElement</code> event, but it will not consume the end tag. * /*from www .j ava 2s . com*/ * @param reader The event stream to read, positioned after the * <code>StartElement</code> * @param consumer The destination for events read from teh stream, or * <code>null</code> to ignore the events completely. * @throws XMLStreamException If an error occurs reading events. */ public static final void copyElementContent(XMLEventReader reader, XMLEventConsumer consumer) throws XMLStreamException { if (!reader.hasNext()) return; for (int depth = 1; true;) { // peek and see if we're at the end element XMLEvent currEvt = reader.peek(); if (currEvt.isEndElement()) { depth--; if (depth == 0) { break; } } else if (currEvt.isStartElement()) { depth++; } // consume the event currEvt = reader.nextEvent(); if (consumer != null) { consumer.add(currEvt); } } }
From source file:Main.java
/** * /*w w w. ja va 2 s . co m*/ * @param elementName * @param attributeValue * @param is * @return Collection * @throws XMLStreamException * @throws FactoryConfigurationError * @throws UnsupportedEncodingException */ public static Collection<String> getElementValues(final String elementName, final String attributeValue, final InputStream is) throws XMLStreamException, UnsupportedEncodingException, FactoryConfigurationError { final Collection<String> elementValues = new ArrayList<>(); final XMLEventReader xmlEventReader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final StringBuffer characters = new StringBuffer(); boolean read = false; while (xmlEventReader.peek() != null) { final XMLEvent event = (XMLEvent) xmlEventReader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && attributeValue != null) { read = false; for (Iterator<Attribute> iterator = event.asStartElement().getAttributes(); iterator .hasNext();) { Attribute attribute = iterator.next(); if (attribute.getValue().equals(attributeValue)) { read = true; break; } } } break; } case XMLStreamConstants.CHARACTERS: { if (read) { characters.append(event.asCharacters().getData()); } break; } case XMLStreamConstants.END_ELEMENT: { if (read) { elementValues.add(characters.toString()); characters.setLength(0); } read = false; break; } default: { // Ignore break; } } } return elementValues; }