FileInputStream fis = null; try { fis = new FileInputStream("test.xml"); XMLInputFactory xmlInFact = XMLInputFactory.newInstance(); XMLStreamReader reader = xmlInFact.createXMLStreamReader(fis); while(reader.hasNext()) { reader.next(); // do something here } } catch(IOException exc) { } catch(XMLStreamException exc) { }
public static void main(String[] args) throws Exception { // Result format Writer diffxResult = new StringWriter(); // Run the diff try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); // a DTD is merely ignored, its presence doesn't cause an exception //java.io.InputStream is = new java.io.ByteArrayInputStream(naive.getBytes("UTF-8")); String simplified = combineAdjacent( inputFactory.createXMLStreamReader(new FileInputStream(new File("tmp_adj.xml"))) ); System.out.println("Done"); } catch (Exception exc) { exc.printStackTrace(); diffxResult = null; } }
public static String getDocumentCharset(final File f) throws IOException, XMLException { XMLEventReader reader = null; try { // using stax to get xml factory objects and read the input file XMLInputFactory inputFactory = XMLInputFactory.newInstance(); reader = inputFactory.createXMLEventReader(new FileInputStream(f)); XMLEvent event = reader.nextEvent(); // Always the first element is StartDocument // even if the XML does not have explicit declaration: StartDocument document = (StartDocument) event; return document.getCharacterEncodingScheme(); } catch (XMLStreamException ex) { throw new XMLException(ex.getMessage(), ex); } finally{ if(reader != null){ try{ reader.close(); } catch(XMLStreamException ex){ LOG.warning(ex.getMessage()); } } } }
/** * Variant of {@link #createDefensiveInputFactory()} with a custom instance. * @param instanceSupplier supplier for the input factory instance * @return a new defensively initialized input factory instance to use * @since 5.0.12 */ public static <T extends XMLInputFactory> T createDefensiveInputFactory(Supplier<T> instanceSupplier) { T inputFactory = instanceSupplier.get(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, false); inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); inputFactory.setXMLResolver(NO_OP_XML_RESOLVER); return inputFactory; }
private static XMLInputFactory createXmlInputFactory() { final XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory(); xmlInputFactory.setProperty(XMLInputFactory.IS_VALIDATING, FALSE); xmlInputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, FALSE); xmlInputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, FALSE); return xmlInputFactory; }
private static SMInputFactory initStax() { XMLInputFactory xmlFactory = XMLInputFactory.newInstance(); xmlFactory.setProperty(XMLInputFactory.IS_COALESCING, Boolean.TRUE); xmlFactory.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, Boolean.FALSE); // just so it won't try to load DTD in if there's DOCTYPE xmlFactory.setProperty(XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); xmlFactory.setProperty(XMLInputFactory.IS_VALIDATING, Boolean.FALSE); return new SMInputFactory(xmlFactory); }
int count = input.read(b); if (count < 3) { return FileFormat.NONE; XMLStreamReader reader = null; try { streamSource = new StreamSource(input); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); reader = inputFactory.createXMLStreamReader(streamSource); reader.nextTag(); if ("netcdf".equals(reader.getName().getLocalPart())) { return FileFormat.NCML; if ("featureCollection".equals(reader.getName().getLocalPart())) { return FileFormat.FC; input.close(); if (streamSource.getInputStream() != null) { streamSource.getInputStream().close();
private QName getRootElementName(File artifact) throws Exception { if (xif == null) { xif = XMLInputFactory.newFactory(); xif.setProperty(XMLInputFactory.IS_NAMESPACE_AWARE, true); } try (InputStream is = new FileInputStream(artifact)) { XMLStreamReader sr = xif.createXMLStreamReader(is); sr.nextTag(); return sr.getName(); } }
/** * Method read * * @param filePath * @param strict */ public AuthenticationKeyDatabase read(String filePath, boolean strict) throws IOException, XMLStreamException { File file = new File(filePath); XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader( file.toURL().toExternalForm(), new FileInputStream(file) ); String encoding = xmlStreamReader.getCharacterEncodingScheme(); AuthenticationKeyDatabase value = parseAuthenticationKeyDatabase( "authenticationKeyDatabase", xmlStreamReader, strict, encoding ); resolveReferences( value ); return value; } //-- AuthenticationKeyDatabase read(String, boolean)
try { is = facesConfig.openStream(); final XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setXMLResolver(NoopXMLResolver.create()); XMLStreamReader parser = inputFactory.createXMLStreamReader(is); StringBuilder phaseListenerName = null; int indent = 0; boolean phaseListener = false; while (true) { int event = parser.next(); if (event == XMLStreamConstants.END_DOCUMENT) { parser.close(); break; indent++; if (indent == 2) { if(parser.getLocalName().equals(LIFECYCLE)){ lifecycle = true; try { if (is != null) { is.close();
in = new FileInputStream(timerFile); final XMLInputFactory inputFactory = INPUT_FACTORY; setIfSupported(inputFactory, XMLInputFactory.IS_VALIDATING, Boolean.FALSE); setIfSupported(inputFactory, XMLInputFactory.SUPPORT_DTD, Boolean.FALSE); final XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in); try { List<TimerImpl> timerList = new ArrayList<>(); if (in != null) { try { in.close(); } catch (IOException e) { EJB3_TIMER_LOGGER.failToCloseFile(e);
final XMLInputFactory xmlInputFactory = StaxHelper.newXMLInputFactory(new XmlOptionsBean(options)); final FileInputStream fis = new FileInputStream(file); final XMLStreamReader rdr = xmlInputFactory.createXMLStreamReader(path, fis); while(!rdr.isStartElement()) { rdr.next(); fis.close();
public static String getEncoding(InputStream is) { XMLStreamReader reader; try { reader = factory.createXMLStreamReader(is); return reader.getEncoding(); } catch (XMLStreamException e) { return null; } finally { try { is.close(); } catch (IOException e) { } } }
XMLInputFactory inputFactory = XMLInputFactory.newInstance(); InputStream in = new FileInputStream("book.xml"); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(in); streamReader.nextTag(); // Advance to "book" element streamReader.nextTag(); // Advance to "person" element while (streamReader.hasNext()) { if (streamReader.isStartElement()) { switch (streamReader.getLocalName()) {
sheetNameIDMap = new LinkedHashMap<String, String>(); workbookData = reader.getWorkbookData(); XMLInputFactory factory = XMLInputFactory.newInstance(); workbookReader = factory.createXMLStreamReader( workbookData ); while ( workbookReader.hasNext() ) { if ( workbookReader.next() == XMLStreamConstants.START_ELEMENT && workbookReader.getLocalName().equals( "sheet" ) ) { String sheetName = workbookReader.getAttributeValue( null, "name" ); String sheetID = workbookReader.getAttributeValue( RELATION_NS_URI, "id" ); workbookData.close(); } catch ( IOException e ) { throw new KettleException( e );
File xmlFile = new File("input.xml"); File textFile = new File("output.txt"); try (InputStream in = new BZip2CompressorInputStream(new FileInputStream(xmlFile)); PrintWriter out = new PrintWriter(new FileWriter(textFile))) { XMLInputFactory factory = XMLInputFactory.newFactory(); XMLStreamReader xmlReader = factory.createXMLStreamReader(in); try { xmlReader.nextTag(); // Read root element, ignore it if (xmlReader.getLocalName().equals("doc")) throw new IllegalArgumentException("Expected root element, found <doc>"); while (xmlReader.nextTag() == XMLStreamConstants.START_ELEMENT) { if (! xmlReader.getLocalName().equals("doc")) throw new IllegalArgumentException("Expected <doc>, found <" + xmlReader.getLocalName() + ">"); String id = xmlReader.getAttributeValue(null, "id"); String url = xmlReader.getAttributeValue(null, "url"); String title = xmlReader.getAttributeValue(null, "title"); String content = xmlReader.getElementText(); // process content value out.println(id + '\t' + url + '\t' + title + '\t' + content); } } finally { xmlReader.close(); } }
private void resetSheetReader() throws IOException, XMLStreamException, InvalidFormatException { sheetReader.close(); sheetStream.close(); sheetStream = xssfReader.getSheet( sheetId ); XMLInputFactory factory = XMLInputFactory.newInstance(); sheetReader = factory.createXMLStreamReader( sheetStream ); }
InputStream is = new FileInputStream("..."); XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(is); while (xsr.hasNext()) { if (xsr.next() == XMLStreamConstants.START_ELEMENT && xsr.getLocalName().equals("dad")) { if (xsr.next() == XMLStreamConstants.CHARACTERS) System.out.println(xsr.getText()); } }
XMLInputFactory inFactory = XMLInputFactory.newInstance(); XMLEventReader eventReader = inFactory.createXMLEventReader(new FileInputStream("1.xml")); XMLOutputFactory factory = XMLOutputFactory.newInstance(); XMLEventWriter writer = factory.createXMLEventWriter(new FileWriter(file)); XMLEventFactory eventFactory = XMLEventFactory.newInstance(); while (eventReader.hasNext()) { XMLEvent event = eventReader.nextEvent(); writer.add(event); if (event.getEventType() == XMLEvent.START_ELEMENT) { if (event.asStartElement().getName().toString().equalsIgnoreCase("book")) { writer.add(eventFactory.createStartElement("", null, "index")); writer.add(eventFactory.createEndElement("", null, "index")); } } } writer.close();
public static XMLStreamReader createSafeXmlTextReader(String url) throws Exception { FileInputStream fis = new FileInputStream(url); XMLStreamReader xtr = factory.createXMLStreamReader(url, fis); return xtr; }