List of usage examples for javax.xml.stream XMLStreamReader next
public int next() throws XMLStreamException;
From source file:Main.java
public static void consumeStart(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException { while (xmlRdr.hasNext()) { if (xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(elementName)) { xmlRdr.next(); return; }//from w w w . j ava2 s .c om xmlRdr.next(); } throw new IllegalStateException( "expected start tag <" + elementName + ">, found '" + xmlRdr.getText() + "'"); }
From source file:Main.java
public static void consumeEnd(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException { while (xmlRdr.hasNext()) { if (xmlRdr.isEndElement() && xmlRdr.getLocalName().equals(elementName)) { xmlRdr.next(); return; }//from w w w . ja va 2 s . co m xmlRdr.next(); } throw new IllegalStateException( "expected start tag <" + elementName + ">, found '" + xmlRdr.getText() + "'"); }
From source file:XmlReaderToWriter.java
public static void writeAll(XMLStreamReader xmlr, XMLStreamWriter writer) throws XMLStreamException { while (xmlr.hasNext()) { write(xmlr, writer);/*from ww w .ja va 2 s . c o m*/ xmlr.next(); } write(xmlr, writer); // write the last element writer.flush(); }
From source file:Main.java
public static void skipToStart(XMLStreamReader xmlRdr, String elementName) throws XMLStreamException { while (xmlRdr.hasNext()) { if (xmlRdr.isStartElement() && xmlRdr.getLocalName().equals(elementName)) { break; }/* w ww. j a v a2 s.c o m*/ xmlRdr.next(); } }
From source file:Main.java
public static void skipElements(XMLStreamReader reader, Integer... elements) throws XMLStreamException { int eventType = reader.getEventType(); List<Integer> types = Arrays.asList(elements); while (types.contains(eventType)) { eventType = reader.next(); }//from ww w.j a v a 2 s . c o m }
From source file:Main.java
/** * Skip current element, including all its content. * Precondition: the current event is START_ELEMENT. * Postcondition: the current event is the corresponding END_ELEMENT. * Similar to {@link XMLStreamReader#nextTag()}, but also skips text content. * @param reader must not be {@code null} * @throws XMLStreamException if the current event is not START_ELEMENT or there is an error processing the underlying XML source *//*from w w w . ja v a 2 s . co m*/ public static void skipElement(XMLStreamReader reader) throws XMLStreamException { if (reader.getEventType() != START_ELEMENT) throw new XMLStreamException("expected start of element", reader.getLocation()); int depth = 0; while (reader.hasNext()) { int event = reader.next(); if (event == START_ELEMENT) { ++depth; } else if (event == END_ELEMENT) { --depth; if (depth == -1) break; } } }
From source file:gdv.xport.util.AbstractFormatterTest.java
/** * We use the XMLStreams to validate the XML. * * @param xmlString XML-String/*from w ww .ja va 2s .c o m*/ * @throws XMLStreamException * the given XML string is not a valid XML */ protected static void checkXML(final String xmlString) throws XMLStreamException { XMLStreamReader xmlr = xmlInputFactory.createXMLStreamReader(new StringReader(xmlString)); int n = 0; while (xmlr.hasNext()) { int eventType = xmlr.next(); if (eventType == XMLStreamConstants.CHARACTERS) { n += xmlr.getText().length(); } } log.info(n + " bytes text in " + xmlString.length() + " bytes XML"); }
From source file:Main.java
/** * /*w w w. j a va2s . c om*/ * Checks indentation (over a single line - multipline text nodes is not supported) * * @param out * @param indentSize * @return * @throws Exception */ public static boolean isIndented(String out, int indentSize) throws Exception { BufferedReader reader = new BufferedReader(new StringReader(out)); boolean indentated = false; int level = 0; int line = 0; String string = reader.readLine(); while (string != null) { int newLevel = 0; while (newLevel < string.length()) { if (!Character.isWhitespace(string.charAt(newLevel))) { break; } newLevel++; } if ((newLevel % indentSize) != 0) { throw new IllegalArgumentException("Unexpected " + newLevel + " whitespace chars at line " + line); } if (Math.abs(level - newLevel) > indentSize) { throw new IllegalArgumentException("Unexpected jump from " + level + " to " + newLevel + " whitespace chars at line " + line + " for indenting with " + indentSize + " chars"); } level = newLevel; string = reader.readLine(); line++; if (level > 0) { indentated = true; } } if (!indentated) { // see if a simple xml piece XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader parser = inputFactory.createXMLStreamReader(new StringReader(out)); int elementMaxLevel = -1; int elementLevel = 0; do { int event = parser.next(); if (event == XMLStreamConstants.START_ELEMENT) { elementLevel++; if (elementMaxLevel < elementLevel) { elementMaxLevel = elementLevel; } } else if (event == XMLStreamConstants.END_ELEMENT) { elementLevel--; } } while (parser.hasNext()); if (elementMaxLevel > 1) { // should be indentated return false; } return true; } return indentated; }
From source file:Main.java
/** * Moves the parser to the next start element. * /* w ww . j av a 2 s.com*/ * @return {@code true} if another start element has been found, * {@code false} otherwise */ public static boolean findNextStartElemenet(XMLStreamReader parser) throws XMLStreamException { assert parser != null; while (true) { int event = parser.getEventType(); if (event == XMLStreamConstants.START_ELEMENT) { return true; } if (parser.hasNext()) { parser.next(); } else { return false; } } }
From source file:fr.inria.oak.paxquery.pact.operators.unary.NavigationOperator.java
/** * /*from www .j ava2s .c o m*/ * @param record * @param treePattern * @param collector */ public static void navigation(NestedMetadata inputRecordSignature, Record record, int column, NavigationTreePattern navigationTreePattern, Collector<Record> collector) { XMLInputFactory factory = XMLInputFactory.newInstance(); try { StringValue value = record.getField(column, StringValue.class); XMLStreamReader streamReader = factory.createXMLStreamReader(new StringReader(value.getValue())); SingleDocumentExtractor extractor = new SingleDocumentExtractor(navigationTreePattern, streamReader); while (streamReader.hasNext()) { streamReader.next(); if (streamReader.getEventType() == XMLStreamConstants.START_ELEMENT) { extractor.startElement(); } else if (streamReader.getEventType() == XMLStreamConstants.END_ELEMENT) { extractor.endElement(); } else if (streamReader.getEventType() == XMLStreamConstants.CHARACTERS) { extractor.characters(); } if (extractor.getRecords().size() != 0) { Iterator<Record> pactRecordsIterator = extractor.getRecords().iterator(); while (pactRecordsIterator.hasNext()) { Record originalRecord = record.createCopy(); RecordOperations.concatenate(originalRecord, pactRecordsIterator.next()); collector.collect(originalRecord); } extractor.getRecords().clear(); } } } catch (XMLStreamException e) { logger.error("XMLStreamException", e); } }