List of usage examples for javax.xml.stream XMLEventReader nextEvent
public XMLEvent nextEvent() throws XMLStreamException;
From source file:org.xmlsh.commands.internal.xml2json.java
private byte[] serializeAsXML(XMLEventReader reader) throws XMLStreamException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); XMLOutputFactory fact = new OutputFactory(); XMLEventWriter writer = fact.createXMLEventWriter(bos, kENCODING_UTF_8); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isEndElement() && event.asEndElement().getName().equals(kELEM_STRING)) break; writer.add(event);/* w w w . j a v a2s . c om*/ } writer.flush(); writer.close(); return bos.toByteArray(); }
From source file:org.xmlsh.commands.internal.xml2json.java
private byte[] serializeAsString(XMLEventReader reader) throws XMLStreamException, UnsupportedEncodingException, IOException { ByteArrayOutputStream bos = new ByteArrayOutputStream(); while (reader.hasNext()) { XMLEvent event = reader.nextEvent(); if (event.isEndElement() && event.asEndElement().getName().equals(kELEM_STRING)) break; if (event.isCharacters()) bos.write(event.asCharacters().getData().getBytes("UTF-8")); }/*from w w w . j a v a2 s . com*/ return bos.toByteArray(); }
From source file:org.xmlsh.commands.internal.xml2json.java
private void readToEnd(XMLEventReader reader) throws XMLStreamException { while (reader.hasNext() && !reader.peek().isEndElement()) reader.nextEvent(); if (reader.hasNext()) reader.nextEvent();/* ww w . j a va 2 s . com*/ }
From source file:org.xmlsh.commands.internal.xml2json.java
private String readChars(XMLEventReader reader) throws XMLStreamException { StringBuffer sb = new StringBuffer(); while (reader.hasNext() && reader.peek().isCharacters()) { Characters ch = reader.nextEvent().asCharacters(); sb.append(ch.getData());// www .ja v a2s . c o m } return sb.toString(); }
From source file:org.zuinnote.hadoop.office.format.common.parser.msexcel.internal.EncryptedCachedDiskStringsTable.java
/** * Reads from the original Excel document the string table and puts it into a * compressed and encrypted file.//from w w w. jav a2s . co m * * @param is * @throws java.io.IOException */ @Override public void readFrom(InputStream is) throws IOException { this.currentItem = 0; // read from source and write into tempfile // open temp file depending on options compressed/encrypted OutputStream tempOS = null; if ((this.ca != null) || (this.compressTempFile)) { tempOS = new FileOutputStream(this.tempFile); if (this.ca != null) { // encrypt file if configured tempOS = new CipherOutputStream(tempOS, this.ciEncrypt); } if (this.compressTempFile) { // compress file if configured tempOS = new GZIPOutputStream(tempOS, EncryptedCachedDiskStringsTable.compressBufferSize); } } else { // not encrypted and not compressed: configure a random access file for // writing/reading = highest performance this.tempRAF = new RandomAccessFile(this.tempFile, "rw"); } // read from source // use Stax event reader XMLEventReader xer = null; try { xer = StaxHelper.newXMLInputFactory().createXMLEventReader(this.originalIS); while (xer.hasNext()) { XMLEvent xe = xer.nextEvent(); // check if it is a string item (entry in string table) if (xe.isStartElement() && xe.asStartElement().getName().getLocalPart().equalsIgnoreCase("si")) { String siText = this.parseSIText(xer); // add string to temp file this.addString(siText, tempOS); this.count++; } } // close tempfile // make tempFile available as a reader } catch (XMLStreamException e) { LOG.error("Cannot read original SharedStringTable from document. Exception " + e); throw new IOException(e); } catch (FormatNotUnderstoodException e) { LOG.error("Cannot read properly SharedStringTable from document. Exception " + e); throw new IOException(e); } finally { // close temporary Stream (tempfile should be deleted using the close method of // this class and not here) if (tempOS != null) { tempOS.close(); } } // open the input stream towards the temp file this.accessTempFile(0L); }
From source file:ru.codeinside.gws.crypto.cryptopro.CryptoProvider.java
private String saxFilter(Node node) { try {//from w w w . j a v a 2 s . c o m final Transformer transformer = TransformerFactory.newInstance().newTransformer(); final StringWriter w1 = new StringWriter(); transformer.transform(new DOMSource(node), new StreamResult(w1)); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLEventReader eventReader = xif .createXMLEventReader(new StreamSource(new StringReader(w1.toString()))); XMLEventReader filteredReader = xif.createFilteredReader(eventReader, new EventFilter() { @Override public boolean accept(XMLEvent event) { int type = event.getEventType(); if (type == XMLStreamConstants.START_DOCUMENT || type == XMLStreamConstants.END_DOCUMENT) { return false; } if (event.isStartElement()) { StartElement startElement = (StartElement) event; QName name = startElement.getName(); if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) { return false; } } if (event.isEndElement()) { EndElement endElement = (EndElement) event; QName name = endElement.getName(); if ("".equals(name.getNamespaceURI()) && "root".equals(name.getLocalPart())) { return false; } } return true; } }); StringWriter sw = new StringWriter(); XMLOutputFactory xof = XMLOutputFactory.newInstance(); XMLEventWriter writer = xof.createXMLEventWriter(sw); while (filteredReader.hasNext()) { writer.add(filteredReader.nextEvent()); } return sw.toString(); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:tds.itemrenderer.processing.ITSDocumentHelper.java
/** * Gets the root element of XML/*from w w w .j av a 2s. c om*/ * * @param uri * @return xml root element * @throws FileNotFoundException * @throws UnsupportedEncodingException * @throws XMLStreamException */ public static String getRootElementName(URI uri) { XMLInputFactory inputFactory = TdsXmlInputFactory.newInstance(); try { InputStream input = null; String uriPath = getUriOriginalString(uri); if (isFtp(uri)) { input = new ByteArrayInputStream(FileFtpHandler.getBytes(uri)); } else { input = new FileInputStream(uriPath); } XMLEventReader inputEventReader = inputFactory.createXMLEventReader(input); while (inputEventReader.hasNext()) { XMLEvent event = inputEventReader.nextEvent(); if (event.isStartElement()) { return event.asStartElement().getName().getLocalPart(); } } } catch (FileNotFoundException | XMLStreamException e) { throw new ITSDocumentProcessingException(e); } return null; }