List of usage examples for javax.xml.stream.util XMLEventConsumer add
public void add(XMLEvent event) throws XMLStreamException;
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. * // w ww . j a v a 2 s . 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 a va 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); } } }