List of usage examples for javax.xml.stream XMLStreamConstants CHARACTERS
int CHARACTERS
To view the source code for javax.xml.stream XMLStreamConstants CHARACTERS.
Click Source Link
From source file:Main.java
private static void parseRestOfDocument(XMLStreamReader reader) throws XMLStreamException { while (reader.hasNext()) { int type = reader.next(); switch (type) { case XMLStreamConstants.START_ELEMENT: System.out.println(reader.getLocalName()); if (reader.getNamespaceURI() != null) { String prefix = reader.getPrefix(); if (prefix == null) { prefix = "[None]"; }//from www . ja va 2 s. c o m System.out.println("prefix = '" + prefix + "', URI = '" + reader.getNamespaceURI() + "'"); } if (reader.getAttributeCount() > 0) { for (int i = 0; i < reader.getAttributeCount(); i++) { System.out.println("Attribute (name = '" + reader.getAttributeLocalName(i) + "', value = '" + reader.getAttributeValue(i) + "')"); String attURI = reader.getAttributeNamespace(i); if (attURI != null) { String attPrefix = reader.getAttributePrefix(i); if (attPrefix == null || attPrefix.equals("")) { attPrefix = "[None]"; } System.out.println("prefix=" + attPrefix + ",URI=" + attURI); } } } break; case XMLStreamConstants.END_ELEMENT: System.out.println("XMLStreamConstants.END_ELEMENT"); break; case XMLStreamConstants.CHARACTERS: if (!reader.isWhiteSpace()) { System.out.println("CD:" + reader.getText()); } break; case XMLStreamConstants.DTD: System.out.println("DTD:" + reader.getText()); break; case XMLStreamConstants.SPACE: System.out.println(" "); break; case XMLStreamConstants.COMMENT: System.out.println(reader.getText()); break; default: System.out.println(type); } } }
From source file:Main.java
/** * /*from w w w .j a v a 2s. 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; }
From source file:MDRevealer.ResistanceTests.java
private static ArrayList<HashMap<String, String>> parse_test(File rt_input) throws XMLStreamException, FileNotFoundException { XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(rt_input)); ArrayList<HashMap<String, String>> test = new ArrayList<HashMap<String, String>>(); HashMap<String, String> hm = new HashMap<String, String>(); String key = "null"; while (xsr.hasNext() == true) { int constant = xsr.next(); switch (constant) { case XMLStreamConstants.START_ELEMENT: key = xsr.getLocalName();// www . ja va2s .c o m if (xsr.getAttributeCount() > 0) { for (int i = 0; i < xsr.getAttributeCount(); i++) { hm.put(xsr.getAttributeLocalName(i), xsr.getAttributeValue(i)); } test.add(hm); hm = new HashMap<String, String>(); } break; case XMLStreamConstants.CHARACTERS: if (!xsr.isWhiteSpace()) { hm.put(key, xsr.getText()); test.add(hm); } hm = new HashMap<String, String>(); break; case XMLStreamConstants.END_ELEMENT: if (xsr.getLocalName().equals("condition")) { hm.put("condition_over", "true"); test.add(hm); hm = new HashMap<String, String>(); } break; } } return test; }
From source file:net.di2e.ecdr.libs.result.relevance.TextParser.java
/** * Given xml as a string, this method will parse out element text and CDATA text. It separates * each by one space character.//from w ww. j av a 2s . c om * * @param xmlData * XML as a {@code String} * @return parsed CDATA and element text */ protected static String parseTextFrom(String xmlData) { StringBuilder builder = new StringBuilder(); XMLStreamReader xmlStreamReader; try { // xml parser does not handle leading whitespace xmlStreamReader = xmlInputFactory.createXMLStreamReader(new StringReader(xmlData)); while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); if (event == XMLStreamConstants.CHARACTERS || event == XMLStreamConstants.CDATA) { String text = xmlStreamReader.getText(); if (StringUtils.isNotBlank(text)) { builder.append(" " + text.trim()); } } if (event == XMLStreamConstants.START_ELEMENT) { for (int i = 0; i < xmlStreamReader.getAttributeCount(); i++) { String text = xmlStreamReader.getAttributeValue(i); if (StringUtils.isNotBlank(text)) { builder.append(" " + text.trim()); } } } } } catch (XMLStreamException e1) { LOGGER.warn("Failure occurred in parsing the xml data (" + xmlData + "). No data has been stored or indexed.", e1); } return builder.toString(); }
From source file:Main.java
/** * // ww w. jav a 2 s . c om * @param elementName * @param is * @param onlyValues * @return Collection * @throws XMLStreamException * @throws UnsupportedEncodingException */ public static Collection<String> getElements(final String elementName, final InputStream is, final boolean onlyValues) throws XMLStreamException, UnsupportedEncodingException { final Collection<String> elements = new ArrayList<>(); final ByteArrayOutputStream os = new ByteArrayOutputStream(); final XMLEventReader reader = XMLInputFactory.newInstance() .createXMLEventReader(new InputStreamReader(is, Charset.defaultCharset().name())); final XMLEventWriter writer = XMLOutputFactory.newInstance() .createXMLEventWriter(new OutputStreamWriter(os, Charset.defaultCharset().name())); boolean read = false; String characters = null; while (reader.peek() != null) { final XMLEvent event = (XMLEvent) reader.next(); switch (event.getEventType()) { case XMLStreamConstants.START_DOCUMENT: case XMLStreamConstants.END_DOCUMENT: { // Ignore. break; } case XMLStreamConstants.START_ELEMENT: { read = read || elementName.equals(event.asStartElement().getName().getLocalPart()); if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.ATTRIBUTE: { if (read && !onlyValues) { writer.add(event); } break; } case XMLStreamConstants.CHARACTERS: { if (read && !onlyValues) { writer.add(event); } characters = event.asCharacters().getData(); break; } case XMLStreamConstants.END_ELEMENT: { if (read && !onlyValues) { writer.add(event); } if (elementName.equals(event.asEndElement().getName().getLocalPart())) { writer.flush(); if (characters != null) { elements.add(characters); } os.reset(); read = false; } break; } default: { // Ignore break; } } } return elements; }
From source file:com.widowcrawler.exo.parse.Parser.java
private static String getEventTypeString(int eventType) { switch (eventType) { case XMLStreamConstants.START_ELEMENT: return "START_ELEMENT"; case XMLStreamConstants.END_ELEMENT: return "END_ELEMENT"; case XMLStreamConstants.PROCESSING_INSTRUCTION: return "PROCESSING_INSTRUCTION"; case XMLStreamConstants.CHARACTERS: return "CHARACTERS"; case XMLStreamConstants.COMMENT: return "COMMENT"; case XMLStreamConstants.START_DOCUMENT: return "START_DOCUMENT"; case XMLStreamConstants.END_DOCUMENT: return "END_DOCUMENT"; case XMLStreamConstants.ENTITY_REFERENCE: return "ENTITY_REFERENCE"; case XMLStreamConstants.ATTRIBUTE: return "ATTRIBUTE"; case XMLStreamConstants.DTD: return "DTD"; case XMLStreamConstants.CDATA: return "CDATA"; case XMLStreamConstants.SPACE: return "SPACE"; case XMLStreamConstants.ENTITY_DECLARATION: return "ENTITY_DECLARATION"; case XMLStreamConstants.NAMESPACE: return "NAMESPACE"; case XMLStreamConstants.NOTATION_DECLARATION: return "NOTATION_DECLARATION"; }// w w w. j a v a 2 s . c o m return "UNKNOWN_EVENT_TYPE: " + eventType; }
From source file:com.rockhoppertech.music.midi.js.xml.ModeFactoryXMLHelper.java
/** * Read modes.xml and create {@code Scale instances} from those definitions. *//*ww w . j a va 2 s . com*/ public static void init() { List<Integer> intervals = null; Scale currentMode = null; String tagContent = null; XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = null; try { reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream("modes.xml")); } catch (XMLStreamException e) { e.printStackTrace(); return; } try { while (reader.hasNext()) { int event = reader.next(); switch (event) { case XMLStreamConstants.START_ELEMENT: String el = reader.getLocalName(); logger.debug("start element '{}'", el); if ("mode".equals(el)) { currentMode = new Scale(); intervals = new ArrayList<>(); } if ("modes".equals(el)) { modeList = new ArrayList<>(); } break; case XMLStreamConstants.CHARACTERS: tagContent = reader.getText().trim(); logger.debug("tagcontent '{}'", tagContent); break; case XMLStreamConstants.END_ELEMENT: switch (reader.getLocalName()) { case "mode": // wow. both guava and commmons to get an int[] array Integer[] array = FluentIterable.from(intervals).toArray(Integer.class); // same as Integer[] array = intervals.toArray(new // Integer[intervals.size()]); int[] a = ArrayUtils.toPrimitive(array); currentMode.setIntervals(a); modeList.add(currentMode); ScaleFactory.registerScale(currentMode); break; case "interval": logger.debug("interval '{}'", tagContent); logger.debug("intervals '{}'", intervals); intervals.add(Integer.parseInt(tagContent)); break; case "name": currentMode.setName(tagContent); break; } break; case XMLStreamConstants.START_DOCUMENT: modeList = new ArrayList<>(); intervals = new ArrayList<>(); break; } } } catch (XMLStreamException e) { logger.error(e.getLocalizedMessage(), e); e.printStackTrace(); } logger.debug("mode list \n{}", modeList); }
From source file:net.kados.gtp.app.libs.Teryt.Subjects.Streets.java
@Override public void extract() throws XMLStreamException, FileNotFoundException { FileInfo ulic = this.bfis.get(Parser.Types.ULIC.toString()); this.factory = XMLInputFactory.newInstance(); this.readerRaw = factory .createXMLStreamReader(new BufferedInputStream(new FileInputStream(ulic.getFile()))); this.reader = factory.createFilteredReader(readerRaw, this.newLineFilter); while (reader.hasNext()) { int next = reader.next(); switch (next) { case XMLStreamConstants.START_ELEMENT: this.newElement(reader.getLocalName()); break; case XMLStreamConstants.CHARACTERS: tagContent = reader.getText().trim(); break; case XMLStreamConstants.END_ELEMENT: this.endElement(reader.getLocalName()); break; }/* w w w. ja va 2 s.c o m*/ if (streets.size() >= 1000) { this.save(); } parserMessages.sendProgress(reader.getLocation().getLineNumber(), ulic.getLines()); } this.save(); }
From source file:fr.inria.oak.paxquery.pact.operators.unary.NavigationOperator.java
/** * //from w w w. j ava 2s. 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); } }
From source file:edu.cmu.lti.oaqa.knn4qa.collection_reader.YahooAnswersStreamParser.java
private ParsedQuestion fetchNext() throws XMLStreamException { ParsedQuestion parsed = null;/*ww w .j ava2 s. com*/ String question = ""; String questDetail = ""; String questUri = ""; String bestAnsw = ""; ArrayList<String> answers = new ArrayList<String>(); boolean bStart = false; String tagContent = null; while (mReader.hasNext()) { switch (mReader.next()) { case XMLStreamConstants.START_ELEMENT: switch (mReader.getLocalName()) { case "vespaadd": bStart = true; break; } break; case XMLStreamConstants.CHARACTERS: tagContent = mReader.getText(); break; case XMLStreamConstants.END_ELEMENT: switch (mReader.getLocalName()) { case "vespaadd": return new ParsedQuestion(question, questDetail, questUri, answers, bestAnsw, mDoCleanUp); case "uri": questUri = tagContent; break; case "subject": question = tagContent; break; case "content": questDetail = tagContent; break; case "bestanswer": bestAnsw = tagContent; break; case "answer_item": answers.add(tagContent); break; } break; } } return null; }