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) { }
private String getJndiNameFromPersistenceXml() throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("META-INF/persistence.xml"); XMLStreamReader reader = factory.createXMLStreamReader(is); while (reader.hasNext()) { int event = reader.next(); if (event == XMLStreamConstants.START_ELEMENT && "jta-data-source".equals(reader.getLocalName())) { return reader.getElementText(); } } return null; } }
private void resetSheetReader() throws IOException, XMLStreamException, InvalidFormatException { sheetReader.close(); sheetStream.close(); sheetStream = xssfReader.getSheet( sheetId ); XMLInputFactory factory = XMLInputFactory.newInstance(); sheetReader = factory.createXMLStreamReader( sheetStream ); }
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; } }
@Override protected void loadResourceXmlFile(XmlContext xmlContext) { Path xmlFile = xmlContext.getXmlFile(); XMLStreamReader xmlStreamReader; try { xmlStreamReader = factory.createXMLStreamReader(Fs.getInputStream(xmlFile)); doParse(xmlStreamReader, xmlContext); } catch (Exception e) { throw new RuntimeException("error parsing " + xmlFile, e); } if (xmlStreamReader != null) { try { xmlStreamReader.close(); } catch (XMLStreamException e) { throw new RuntimeException(e); } } }
this.clazz = clazz; this.unmarshaller = JAXBContext.newInstance(clazz).createUnmarshaller(); this.reader = XMLInputFactory.newInstance().createXMLStreamReader(stream); return reader.hasNext(); reader.close(); eventType = reader.next();
/** * Need to override the XML comparison in the case where the input xsd and the output xsd differ. * * @param configId the id of the xml configuration * @param original the original subsystem xml * @param marshalled the marshalled subsystem xml * @throws Exception */ @Override protected void compareXml(String configId, final String original, final String marshalled) throws Exception { final XMLStreamReader originalReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(original)); final XMLStreamReader marshalledReader = XMLInputFactory.newInstance().createXMLStreamReader(new StringReader(marshalled)); String originalNS = null; if (originalReader.next() == XMLStreamConstants.START_ELEMENT) { originalNS = originalReader.getNamespaceURI(); } String marshalledNS = null; if (marshalledReader.next() == XMLStreamConstants.START_ELEMENT) { marshalledNS = marshalledReader.getNamespaceURI(); } // only compare if namespace URIs are the same if (originalNS.equals(marshalledNS)) { compareXml(configId, original, marshalled, true); } }
rd = INPUT_FACTORY.createXMLStreamReader( new StringReader( xml ) ); boolean wasStart = false; boolean wasSomething = false; while ( rd.hasNext() ) { int event = rd.next(); if ( event != XMLStreamConstants.CDATA && cdata.length() > 0 ) { rd.close();
try { final XMLInputFactory factory = XMLInputFactory.newInstance(); factory.setProperty(XMLInputFactory.SUPPORT_DTD, false); // disable DTDs entirely for that factory factory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, false); // disable external entities final XMLStreamReader xmlReader; if (charEncoding != null) { xmlReader = factory.createXMLStreamReader(stream, charEncoding); } else { xmlReader = factory.createXMLStreamReader(stream); if (!"Envelope".equals(xmlReader.getLocalName())) { LOG.debug("Unexpected first tag of SOAP request: '" + xmlReader.getLocalName() + "' (expected 'Envelope')"); return null; //failed return "." + xmlReader.getLocalName(); } catch (final XMLStreamException e) { LOG.debug("Unable to parse SOAP request", e);
XMLInputFactory f = XMLInputFactory.newInstance(); XMLStreamReader rdr = f.createXMLStreamReader(new FileReader("test.xml")); while (rdr.hasNext()) { if (rdr.next() == XMLStreamConstants.START_ELEMENT) { if (rdr.getLocalName().equals("FamilyAnnualDeductibleAmount")) { rdr.nextTag(); int familyAnnualDeductibleAmount = Integer.parseInt(rdr.getElementText()); System.out.println("familyAnnualDeductibleAmount = " + familyAnnualDeductibleAmount); } else if (rdr.getLocalName().equals("FamilyAnnualOOPLimitAmount")) { rdr.nextTag(); int familyAnnualOOPLimitAmount = Integer.parseInt(rdr.getElementText()); System.out.println("FamilyAnnualOOPLimitAmount = " + familyAnnualOOPLimitAmount); } } } rdr.close();
@Test @Override @SuppressWarnings("unchecked") public void unmarshalPartialStaxSourceXmlStreamReader() throws Exception { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING)); streamReader.nextTag(); // skip to flights streamReader.nextTag(); // skip to flight Source source = StaxUtils.createStaxSource(streamReader); JAXBElement<FlightType> element = (JAXBElement<FlightType>) unmarshaller.unmarshal(source); FlightType flight = element.getValue(); testFlight(flight); }
try { streamSource = new StreamSource(input); XMLInputFactory inputFactory = XMLInputFactory.newInstance(); reader = inputFactory.createXMLStreamReader(streamSource); reader.nextTag(); if ("netcdf".equals(reader.getName().getLocalPart())) { reader.close(); } catch (XMLStreamException e) {
xmlInputFactory.createXMLStreamReader(path, fis); rdr.next();
public boolean isAtom(String url) throws ParserConfigurationException, SAXException, IOException{ XMLInputFactory xif = XMLInputFactory.newFactory(); XMLStreamReader xsr = xif.createXMLStreamReader(new URL(url).openConnection()); xsr.nextTag(); // Advance to root element return xsr.getLocalName().equals("feed") && xsr.getNamespaceURI().equals("http://www.w3.org/2005/Atom"); }
XMLStreamReader xsr = xif.createXMLStreamReader(new StreamSource(new File("file.xml"))); xsr.close();
private void openXMLStream(InputStream in) throws XMLStreamException { XMLInputFactory factory = XMLInputFactory.newInstance(); parser = factory.createXMLStreamReader(in, "UTF-8"); int event = parser.next(); if (event != XMLStreamConstants.START_ELEMENT || !parser.getLocalName().equalsIgnoreCase("osm")) { throw new IllegalArgumentException("File is not a valid OSM stream"); } // See https://wiki.openstreetmap.org/wiki/PBF_Format#Definition_of_the_OSMHeader_fileblock String timestamp = parser.getAttributeValue(null, "osmosis_replication_timestamp"); if (timestamp == null) timestamp = parser.getAttributeValue(null, "timestamp"); if (timestamp != null) { try { fileheader = new OSMFileHeader(); fileheader.setTag("timestamp", timestamp); } catch (Exception ex) { } } eof = false; }
private Source readStAXSource(InputStream body, HttpInputMessage inputMessage) { try { XMLInputFactory inputFactory = XMLInputFactory.newInstance(); inputFactory.setProperty(XMLInputFactory.SUPPORT_DTD, isSupportDtd()); inputFactory.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES, isProcessExternalEntities()); if (!isProcessExternalEntities()) { inputFactory.setXMLResolver(NO_OP_XML_RESOLVER); } XMLStreamReader streamReader = inputFactory.createXMLStreamReader(body); return new StAXSource(streamReader); } catch (XMLStreamException ex) { throw new HttpMessageNotReadableException( "Could not parse document: " + ex.getMessage(), ex, inputMessage); } }
private SingletonDeploymentConfiguration parse(DeploymentUnit unit, File file) throws DeploymentUnitProcessingException { try (BufferedReader reader = Files.newBufferedReader(file.toPath(), StandardCharsets.UTF_8)) { XMLStreamReader xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(reader); try { MutableSingletonDeploymentConfiguration config = new MutableSingletonDeploymentConfiguration(unit); this.mapper.parseDocument(config, xmlReader); return config; } finally { xmlReader.close(); } } catch (XMLStreamException e) { throw ServerLogger.ROOT_LOGGER.errorLoadingDeploymentStructureFile(file.getPath(), e); } catch (FileNotFoundException e) { throw ServerLogger.ROOT_LOGGER.deploymentStructureFileNotFound(file); } catch (IOException e) { throw ServerLogger.ROOT_LOGGER.deploymentStructureFileNotFound(file); } } }
private static boolean _containsStagedModel( PortletDataContext portletDataContext, String path, StagedModel stagedModel) { XMLInputFactory xmlInputFactory = SecureXMLFactoryProviderUtil.newXMLInputFactory(); try { XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader( portletDataContext.getZipEntryAsInputStream(path)); Class<?> modelClass = stagedModel.getModelClass(); String simpleName = modelClass.getSimpleName(); while (xmlStreamReader.hasNext()) { int event = xmlStreamReader.next(); if (event == XMLStreamConstants.START_ELEMENT) { QName qName = xmlStreamReader.getName(); if (Objects.equals(qName.getLocalPart(), simpleName)) { return true; } } } } catch (XMLStreamException xmlse) { if (_log.isDebugEnabled()) { _log.debug(xmlse, xmlse); } } return false; }
private XMLStreamReader getEmptyBody() throws XMLStreamException{ // create an empty body and create xmlstream reader out of it String emptyBody = "<S:Body xmlns:S=\""+ soapVersion.nsUri +"\""+"></S:Body>"; InputStream in = new ByteArrayInputStream(emptyBody.getBytes()); XMLInputFactory xif = XMLInputFactory.newInstance(); XMLStreamReader empBody = xif.createXMLStreamReader(in); empBody.next(); //next of start document empBody.next(); // next of Body, will point to end of body return empBody; } private XMLStreamReader getEmptyBodyNoException() {