List of usage examples for javax.xml.stream XMLEventReader hasNext
@Override public boolean hasNext();
From source file:EventFilterExample.java
private static int countEvents(XMLEventReader reader) throws XMLStreamException { int counter = 0; while (reader.hasNext()) { reader.next();//from ww w . j a v a 2 s . c o m counter++; } return counter; }
From source file:Main.java
private static String flattenXmlRecord(final String xmlRecord) throws XMLStreamException { StringBuilder flatXmlRecord = new StringBuilder(); XMLEventReader eventReader = inFactory.createXMLEventReader(new StringReader(xmlRecord)); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); if (event.getEventType() != XMLEvent.START_ELEMENT) { continue; }/*from w w w . j a v a 2s .co m*/ String elementName = event.asStartElement().getName().getLocalPart(); event = eventReader.nextEvent(); if (event.getEventType() != XMLEvent.CHARACTERS) { continue; } if (event.asCharacters().getData().trim().isEmpty()) { continue; } flatXmlRecord.append(elementName + ", " + event.asCharacters().getData() + "\n"); } return flatXmlRecord.toString(); }
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.ja v a 2 s.c o m * * @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
/** * Returns the name of the root element of an XML message. * /*from ww w . j a va 2 s.com*/ * @param xml XML message * @return the name of the root element * @exception Exception if an error occurs */ public static String getRootElementName(byte[] xml) throws Exception { ByteArrayInputStream is = new ByteArrayInputStream(xml); XMLEventReader reader = inputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = (XMLEvent) reader.next(); if (event.isStartElement()) { StartElement startElement = event.asStartElement(); return startElement.getName().getLocalPart(); } } reader.close(); return ""; }
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. * //from w w w .jav a 2 s.c om * @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. * /*from w w w. j a v a 2s . 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
/** * Describe <code>getInternalXML</code> method here. * * @param is an <code>InputStream</code> value * @param tagName a <code>String</code> value * @param namespace a <code>String</code> value * @return a <code>byte[]</code> value * @exception Exception if an error occurs *///from ww w. j a v a 2 s .c o m public static byte[] getInternalXML(InputStream is, String tagName, String namespace) throws Exception { if (namespace == null) namespace = ""; ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLEventWriter writer = outputFactory.createXMLEventWriter(bos); // Loop over XML input stream and process events boolean bStartWritingEvents = false; XMLEventReader reader = inputFactory.createXMLEventReader(is); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (checkNameAndNamespace(event, tagName, namespace) == true) { if (event.isStartElement() && !bStartWritingEvents) { writer.add(eventFactory.createStartDocument()); bStartWritingEvents = true; continue; } else if (event.isEndElement()) { writer.add(eventFactory.createEndDocument()); bStartWritingEvents = false; break; } } if (bStartWritingEvents) { writer.add(event); } } writer.close(); reader.close(); return bos.toByteArray(); }
From source file:Main.java
/** * Copy an XML event stream./* ww w. ja v a 2 s . c om*/ * @param reader The event reader. * @param writer The event writer. * @param omitDoc if true, ignore start/end document events. * @throws XMLStreamException For errors writing to the XML event writer. */ public static void copyXMLEventStream(final XMLEventReader reader, final XMLEventWriter writer, final boolean omitDoc) throws XMLStreamException { if (omitDoc) { while (reader.hasNext()) { final XMLEvent event = reader.nextEvent(); final int type = event.getEventType(); if ((type != XMLStreamConstants.START_DOCUMENT) && (type != XMLStreamConstants.END_DOCUMENT)) { writer.add(event); } } } else { writer.add(reader); } writer.flush(); }
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 w w w .j av a 2s .c o m*/ * @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
/** * 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 w w w .j a v a2 s . co 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()); }