List of usage examples for javax.xml.stream XMLStreamReader close
public void close() throws XMLStreamException;
From source file:FISTest.java
public static void main(String[] args) throws Exception { XMLInputFactory factory = XMLInputFactory.newInstance(); FileInputStream fis = new FileInputStream("c.xml"); XMLStreamReader reader = factory.createXMLStreamReader(fis); reader.close(); fis.close();//ww w . j ava 2 s .c o m }
From source file:Main.java
public static void closeStaxStreamReader(XMLStreamReader xsr, FileReader fr) { try {/*ww w .j av a 2 s . c o m*/ if (xsr != null) { xsr.close(); } if (fr != null) { fr.close(); } } catch (XMLStreamException ex) { // } catch (IOException ex) { // } }
From source file:com.adaptris.core.services.UseXmlCharsetAsEncodingService.java
private static void closeQuietly(XMLStreamReader r) { try {/*from w ww . j a v a 2 s . com*/ if (r != null) { r.close(); } } catch (Exception ignored) { } }
From source file:Main.java
public static boolean hasValidXmlDeclaration(byte[] doc) throws Exception { try {// www . j av a 2 s. com XMLStreamReader r = xif.createXMLStreamReader(new ByteArrayInputStream(doc), null); if (!"UTF-8".equalsIgnoreCase(r.getEncoding())) return false; if (!"1.0".equals(r.getVersion())) return false; r.close(); return true; } catch (XMLStreamException e) { throw new Exception("Unable to parse xml", e); } }
From source file:com.wavemaker.commons.util.IOUtils.java
public static void closeXmlReaderSilently(XMLStreamReader reader) { if (reader != null) { try {//from w w w. j av a 2 s.c o m reader.close(); } catch (Exception exc) { } } }
From source file:net.cloudkit.enterprises.ws.SuperPassQueryTest.java
public static String parsingReceiptStatus(String responseContext) throws XMLStreamException { String serviceResponseCode = "-1"; XMLStreamReader reader = factory.createXMLStreamReader(new StringReader(responseContext)); try {// w w w . ja va 2s . c om int event = reader.getEventType(); while (true) { switch (event) { case XMLStreamConstants.START_ELEMENT: // System.out.println(reader.getName()); if (reader.getName().toString().equals("ServiceResponseCode")) { // System.out.println(reader.getElementText()); serviceResponseCode = reader.getElementText(); } break; case XMLStreamConstants.END_ELEMENT: // System.out.println("End Element:" + r.getName()); break; } if (!reader.hasNext()) break; event = reader.next(); } } finally { reader.close(); } return serviceResponseCode; }
From source file:com.microsoft.tfs.core.memento.XMLMemento.java
/** * Reads an {@link XMLMemento} from the next XML element in the given given * {@link InputStream} in the encoding specified as * {@link #DEFAULT_ENCODING}.// w ww .j a va 2s. c o m * * @param inputStream * the {@link InputStream} read to read the {@link XMLMemento} from * (must not be <code>null</code>) * @param encoding * the encoding to use when reading the {@link InputStream}, * <code>null</code> to use the default encoding ( * {@link #DEFAULT_ENCODING}) * @return a Memento modeled as the first Element in the document. * @throws MementoException * if an error prevented the creation of the Memento. */ public static XMLMemento read(final InputStream inputStream, final String encoding) throws MementoException { Check.notNull(inputStream, "inputStream"); //$NON-NLS-1$ try { final XMLStreamReader reader = StaxFactoryProvider.getXMLInputFactory(true) .createXMLStreamReader(inputStream, (encoding != null) ? encoding : DEFAULT_ENCODING); XMLMemento memento = null; String localName; int event; do { event = reader.next(); if (event == XMLStreamConstants.START_ELEMENT) { localName = reader.getLocalName(); memento = new XMLMemento(localName); memento.readFromElement(reader); } } while (event != XMLStreamConstants.END_ELEMENT && event != XMLStreamConstants.END_DOCUMENT); reader.close(); return memento; } catch (final XMLStreamException e) { log.error("Error reading", e); //$NON-NLS-1$ throw new MementoException(e); } }
From source file:org.deegree.protocol.ows.http.OwsHttpResponseImpl.java
@Override public void assertNoXmlContentTypeAndExceptionReport() throws OWSExceptionReport, XMLStreamException { Header contentType = httpResponse.getFirstHeader("Content-Type"); if (contentType != null && contentType.getValue().startsWith("text/xml")) { XMLStreamReader xmlStream = xmlFac.createXMLStreamReader(url, is); try {/* www .j a v a 2 s.co m*/ assertNoExceptionReport(xmlStream); } finally { xmlStream.close(); } } }
From source file:com.gtdfree.test.XMLTest.java
public void testParserEncoding() { try {//from w ww.j a v a2s . c o 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()); } }
From source file:org.deegree.protocol.ows.http.OwsHttpResponseTest.java
/** * Test method for {@link org.deegree.protocol.ows.http.OwsHttpResponse#getAsXMLStream()}. *///from w ww . jav a 2 s. co 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); }