List of usage examples for javax.xml.stream XMLStreamReader next
public int next() throws XMLStreamException;
From source file:wiki.link.LinkImporter.java
public boolean readXML(String filename) { //System.exit(1); //MAKE BLOODY SURE YOU HAVE SOME HOURS. DbConnector dbc = new DbConnector("localhost"); dbc.jdbcTemplate.update("TRUNCATE links;"); try {/*from ww w.j a v a2s . c o m*/ XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader xsr = xif.createXMLStreamReader(new FileInputStream(filename)); long n = 0; List<Doc> toSave = new ArrayList<>(); while (xsr.hasNext()) { xsr.next(); if (xsr.getEventType() == XMLStreamReader.START_ELEMENT) { if (xsr.getLocalName().equals("page")) { long id = -1; String title = null; String text = null; while (xsr.hasNext()) { xsr.next(); if (xsr.getEventType() == XMLStreamReader.START_ELEMENT) { if (xsr.getLocalName().equals("id") && id == -1) { id = Long.parseLong(xsr.getElementText()); } if (xsr.getLocalName().equals("title")) { title = xsr.getElementText(); } if (xsr.getLocalName().equals("text")) { text = xsr.getElementText(); } } else if (xsr.getEventType() == XMLStreamReader.END_ELEMENT && xsr.getLocalName().equals("page")) { break; } } if (id != -1 && title != null && text != null) { Doc wd = new Doc(id, title, text); toSave.add(wd); n++; if (n % 1000 == 0) { insertLinks(toSave, dbc); // WikiDoc.insertAll(toSave, dbc); System.out.println(n); toSave.clear(); } } } } } } catch (IOException | XMLStreamException e) { e.printStackTrace(); } return true; }
From source file:edu.utah.further.core.xml.xquery.UTestXQueryServiceBaseX.java
/** * Execute a test XQuery document and put the results in a Stream. * //from w w w . j a v a 2 s.c o m * @throws XMLStreamException */ @Test public void executeXQueryIntoStream() throws XMLStreamException { final XMLStreamReader result = xQueryService.executeIntoStream(testXQuery, testXmlDocument, new HashMap<String, String>()); assertNotNull(result); String elementResult = null; while (result.hasNext()) { if (result.next() == XMLStreamReader.START_ELEMENT && result.getName().getLocalPart() == "message") { elementResult = result.getElementText(); } } assertNotNull(elementResult); assertThat(elementResult, is("Hello World!")); }
From source file:hudson.model.ExternalRun.java
/** * Instead of performing a build, accept the log and the return code from a * remote machine./* w ww . j a va 2 s.c o m*/ * * <p> The format of the XML is: * * <pre><xmp> * <run> * <log>...console output...</log> * <result>exit code</result> * </run> * </xmp></pre> */ @SuppressWarnings({ "Since15" }) public void acceptRemoteSubmission(final Reader in) throws IOException { final long[] duration = new long[1]; run(new Runner() { private String elementText(XMLStreamReader r) throws XMLStreamException { StringBuilder buf = new StringBuilder(); while (true) { int type = r.next(); if (type == CHARACTERS || type == CDATA) { buf.append(r.getTextCharacters(), r.getTextStart(), r.getTextLength()); } else { return buf.toString(); } } } public Result run(BuildListener listener) throws Exception { PrintStream logger = null; try { logger = new PrintStream(new DecodingStream(listener.getLogger())); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader p = xif.createXMLStreamReader(in); p.nextTag(); // get to the <run> p.nextTag(); // get to the <log> charset = p.getAttributeValue(null, "content-encoding"); while (p.next() != END_ELEMENT) { int type = p.getEventType(); if (type == CHARACTERS || type == CDATA) { logger.print(p.getText()); } } p.nextTag(); // get to <result> Result r = Integer.parseInt(elementText(p)) == 0 ? Result.SUCCESS : Result.FAILURE; p.nextTag(); // get to <duration> (optional) if (p.getEventType() == START_ELEMENT && p.getLocalName().equals("duration")) { duration[0] = Long.parseLong(elementText(p)); } return r; } finally { IOUtils.closeQuietly(logger); } } public void post(BuildListener listener) { // do nothing } public void cleanUp(BuildListener listener) { // do nothing } }); if (duration[0] != 0) { super.duration = duration[0]; // save the updated duration save(); } }
From source file:net.bulletin.pdi.xero.step.support.XMLChunkerImpl.java
@Override public String pullNextXmlChunk() throws KettleException { Stack<String> elementStack = xmlChunkerState.getElementStack(); XMLStreamReader xmlStreamReader = xmlChunkerState.getXmlStreamReader(); try {//from www . j a v a 2 s . co m while (xmlStreamReader.hasNext()) { switch (xmlStreamReader.next()) { case XMLStreamConstants.END_DOCUMENT: return null; case XMLStreamConstants.END_ELEMENT: elementStack.pop(); break; case XMLStreamConstants.START_ELEMENT: elementStack.push(xmlStreamReader.getLocalName()); if (actualElementStackHasExpectedElements(xmlChunkerState)) { return pullNextXmlChunkFromTopElementOnStack(xmlChunkerState); } break; } } } catch (Exception e) { throw new KettleException("a problem has arisen reading the xero xml stream", e); } return null; }
From source file:edu.indiana.d2i.htrc.io.index.solr.SolrClient.java
private NamedVector parseOneVolume(InputStream content) throws XMLStreamException, IOException { // java.io.BufferedReader br = new java.io.BufferedReader(new java.io.InputStreamReader(content)); // String line = ""; // while ((line = br.readLine()) != null) { // System.out.println(line); // }//from ww w . j a v a2 s .co m // br.close(); String volumeID = null; Vector vector = null; XMLStreamReader parser = factory.createXMLStreamReader(content); while (parser.hasNext()) { int event = parser.next(); if (event == XMLStreamConstants.START_ELEMENT) { String attributeValue = parser.getAttributeValue(null, "name"); if (attributeValue != null) { if (attributeValue.equals(VOLUME_ID)) { volumeID = parser.getElementText(); volumeID = pairtree.uncleanId(volumeID); } else if (attributeValue.equals(VOLUME_OCR)) { vector = createVector(parser); break; } } } } NamedVector tv = new NamedVector(vector, volumeID); return tv; }
From source file:org.deegree.protocol.ows.http.OwsHttpResponseTest.java
/** * Test method for {@link org.deegree.protocol.ows.http.OwsHttpResponse#getAsXMLStream()}. */// w w w. j a v a 2 s . c o m @Test public void testGetAsXMLStreamScenario1() throws OWSExceptionReport, XMLStreamException { XMLStreamReader xmlStream = scenario1.getAsXMLStream(); int i = 0; while (xmlStream.hasNext()) { xmlStream.next(); i++; } xmlStream.close(); assertEquals(8215, i); }
From source file:com.sazneo.export.file.FileProcessor.java
/** * Process the XML file/* ww w .j a v a 2 s. c o m*/ */ public void process() { try { Reader reader = new FileReader(exportFile); XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); XMLStreamReader xmlStreamReader = xmlFactory.createXMLStreamReader(reader); boolean fileTag = false; String fileName = null; Integer fileSize = null; while (xmlStreamReader.hasNext()) { xmlStreamReader.next(); if (xmlStreamReader.getEventType() == XMLStreamReader.START_ELEMENT) { if ("file".equals(xmlStreamReader.getLocalName())) { fileTag = true; fileName = MessageFormat.format("{0}_{1}", xmlStreamReader.getAttributeValue(null, "id"), xmlStreamReader.getAttributeValue(null, "filename")); fileSize = Integer.parseInt(xmlStreamReader.getAttributeValue(null, "size")); logger.debug(MessageFormat.format("XML contains file with name: {0} and size {1} bytes", fileName, fileSize)); } } if (fileTag) { if (xmlStreamReader.getEventType() == XMLStreamReader.CHARACTERS) { exportFile(xmlStreamReader.getText(), fileName, fileSize); } if (xmlStreamReader.getEventType() == XMLStreamReader.END_ELEMENT) { fileSize = null; fileName = null; fileTag = false; } } } } catch (FileNotFoundException e) { logger.error(MessageFormat.format("Could not find export file:-\n{0}", e)); } catch (XMLStreamException e) { logger.error(MessageFormat.format("Could not parse xml export file:-\n{0}", e)); } }
From source file:fr.mobilit.neo4j.server.service.nantes.CycleRentTest.java
@BeforeClass public void setUp() throws Exception { super.setUp(true); HttpClient client = new HttpClient(); GetMethod get = null;/*www .j av a 2 s .c o m*/ try { // we do the http call and parse the xml response get = new GetMethod(CycleRentImpl.IMPORT_URL); client.executeMethod(get); javax.xml.stream.XMLInputFactory factory = javax.xml.stream.XMLInputFactory.newInstance(); javax.xml.stream.XMLStreamReader parser = factory.createXMLStreamReader(get.getResponseBodyAsStream()); ArrayList<String> currentXMLTags = new ArrayList<String>(); int depth = 0; while (true) { int event = parser.next(); if (event == javax.xml.stream.XMLStreamConstants.END_DOCUMENT) { break; } switch (event) { case javax.xml.stream.XMLStreamConstants.START_ELEMENT: currentXMLTags.add(depth, parser.getLocalName()); String tagPath = currentXMLTags.toString(); // here we have a match, so we construct the POI if (tagPath.equals("[carto, markers, marker]")) { numberOfStations++; } depth++; break; case javax.xml.stream.XMLStreamConstants.END_ELEMENT: depth--; currentXMLTags.remove(depth); break; default: break; } } } catch (Exception e) { throw new RuntimeException(e.getMessage(), e.getCause()); } finally { get.releaseConnection(); } }
From source file:au.org.ala.bhl.DocumentPaginator.java
private void paginateImpl(XMLStreamReader parser, PageHandler handler) throws Exception { if (parser == null) { return;/*w w w.j a v a 2 s . c o m*/ } StringBuilder buffer = new StringBuilder(); String currentPage = null; while (true) { int event = parser.next(); if (event == XMLStreamConstants.END_DOCUMENT) { parser.close(); break; } if (event == XMLStreamConstants.START_ELEMENT) { if (parser.getLocalName().equals("PARAM")) { String attrName = parser.getAttributeValue("", "name"); if (attrName.equals("PAGE")) { if (!StringUtils.isEmpty(currentPage)) { if (handler != null) { handler.handlePage(currentPage, buffer.toString()); } } buffer = new StringBuilder(); currentPage = parser.getAttributeValue("", "value"); } } } if (event == XMLStreamConstants.CHARACTERS) { String value = StringUtils.trim(parser.getText()); if (!StringUtils.isEmpty(value)) { buffer.append(value).append(" "); } } } }
From source file:com.gtdfree.test.XMLTest.java
public void testParserEncoding() { try {/* ww w . j av a 2 s . co m*/ File file = new File("./src/test/resources/gtd-free-data_2.1.xml"); InputStream is = new FileInputStream(file); XMLStreamReader r = XMLInputFactory.newInstance().createXMLStreamReader(is); System.out.println(r.getEncoding()); assertEquals("UTF-8", r.getEncoding()); while (r.hasNext()) { r.next(); } r.close(); is.close(); file = new File("./src/test/resources/gtd-free-data_WIN1250_2.1.xml"); is = new FileInputStream(file); r = XMLInputFactory.newInstance().createXMLStreamReader(is); System.out.println(r.getEncoding()); assertEquals("UTF-8", r.getEncoding()); try { while (r.hasNext()) { r.next(); } fail("This should not happend."); } catch (Exception e) { //e.printStackTrace(); } r.close(); is.close(); file = new File("./src/test/resources/gtd-free-data_2.1_enc.xml"); is = new FileInputStream(file); r = XMLInputFactory.newInstance().createXMLStreamReader(is); System.out.println(r.getEncoding()); assertEquals("UTF-8", r.getEncoding()); while (r.hasNext()) { r.next(); } r.close(); is.close(); file = new File("./src/test/resources/gtd-free-data_WIN1250_2.1_enc.xml"); is = new FileInputStream(file); r = XMLInputFactory.newInstance().createXMLStreamReader(is); System.out.println(r.getEncoding()); assertEquals("WINDOWS-1250", r.getEncoding()); while (r.hasNext()) { r.next(); } r.close(); is.close(); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }