class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> { private Exception exception; protected RSSFeed doInBackground(String... urls) { try { URL url = new URL(urls[0]); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); RssHandler theRSSHandler = new RssHandler(); xmlreader.setContentHandler(theRSSHandler); InputSource is = new InputSource(url.openStream()); xmlreader.parse(is); return theRSSHandler.getFeed(); } catch (Exception e) { this.exception = e; return null; } } protected void onPostExecute(RSSFeed feed) { // TODO: check this.exception // TODO: do something with the feed } }
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); XMLReader xr = factory.newSAXParser().getXMLReader(); xr.setContentHandler(this); xr.setErrorHandler(this); xr.parse(new InputSource(r)); } finally { if (r != null){
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); try { SAXParser saxParser = factory.newSAXParser(); File file = new File("test.xml"); saxParser.parse(file, new ElementHandler()); // specify handler } catch(ParserConfigurationException e1) { } catch(SAXException e1) { } catch(IOException e) { }
private static SAXParser newSAXParser(boolean namespaceAware, boolean validating, Schema schema1) throws ParserConfigurationException, SAXException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(validating); factory.setNamespaceAware(namespaceAware); factory.setSchema(schema1); return factory.newSAXParser(); }
protected void loadXML() throws IOException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); XMLReader xr = factory.newSAXParser().getXMLReader(); rules = new HashMap<String, JSGFRule>(); GrXMLHandler handler = new GrXMLHandler(baseURL, rules, logger); xr.setContentHandler(handler); xr.setErrorHandler(handler); InputStream is = baseURL.openStream(); xr.parse(new InputSource(is)); is.close(); } catch (SAXParseException e) { String msg = "Error while parsing line " + e.getLineNumber() + " of " + baseURL + ": " + e.getMessage(); throw new IOException(msg); } catch (SAXException e) { throw new IOException("Problem with XML: " + e); } catch (ParserConfigurationException e) { throw new IOException(e.getMessage()); } return; }
isource = SAXSource.sourceToInputSource(source); baseID = isource.getSystemId(); javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser(); reader = jaxpParser.getXMLReader(); throw new org.xml.sax.SAXException(ex); throw new org.xml.sax.SAXException(ex1.toString()); reader.setContentHandler(handler); reader.parse(isource);
/** * Create new {@link javax.xml.transform.sax.SAXSource} for a given entity input stream. * * @param spf SAX parser factory to be used to create the SAX source. * @param entityStream entity input stream. * @return new {@link javax.xml.transform.sax.SAXSource} representing the entity input stream. * @throws JAXBException in case SAX source creation fails. */ protected static SAXSource getSAXSource(SAXParserFactory spf, InputStream entityStream) throws JAXBException { try { return new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(entityStream)); } catch (Exception ex) { throw new JAXBException("Error creating SAXSource", ex); } }
private static void parseXml(InputStream in, String systemId, DefaultHandler handler) throws SAXException, IOException { InputSource inSrc = new InputSource(); inSrc.setSystemId(systemId); inSrc.setByteStream(toCloseIgnoring(in)); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); // Especially as we use dummy empty DTD-s XMLReader reader; try { reader = factory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { // Not expected throw new RuntimeException("XML parser setup failed", e); } reader.setEntityResolver(new EmptyContentEntityResolver()); // To deal with referred DTD-s reader.setContentHandler(handler); reader.setErrorHandler(handler); reader.parse(inSrc); }
SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); SAXParser parser = factory.newSAXParser(); XMLReader reader = parser.getXMLReader(); reader.setErrorHandler(new SimpleErrorHandler()); reader.parse(new InputSource(new FileReader ("document.xml")));
/** * Loads a set of configuration data from the location * * @return a map keyed by component name containing RawPropertyData objects * @throws IOException if an I/O or parse error occurs */ public Map<String, RawPropertyData> load() throws IOException { try { SAXParserFactory factory = SAXParserFactory.newInstance(); XMLReader xr = factory.newSAXParser().getXMLReader(); ConfigHandler handler = new ConfigHandler(rpdMap, globalProperties, replaceDuplicates, url); xr.setContentHandler(handler); xr.parse(url.toString()); } catch (SAXParseException e) { String msg = "Error while parsing line " + e.getLineNumber() + " of " + url + ": " + e.getMessage(); throw new IOException(msg); } catch (SAXException e) { throw new IOException("Problem with XML: " + e); } catch (ParserConfigurationException e) { throw new IOException(e.getMessage()); } return rpdMap; } }
/** * Indents the given XML String. * * @param r A reader on XML data * @param w A writer for the indented XML * * @throws IOException If an IOException occurs. * @throws SAXException If the XML is not well-formed. * @throws ParserConfigurationException If the parser could not be configured */ public static void indent(Reader r, Writer w) throws SAXException, IOException, ParserConfigurationException { // create the indenter XMLIndenter indenter = new XMLIndenter(w); // initialise the SAX framework SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); InputSource source = new InputSource(r); // parse the XML SAXParser parser = factory.newSAXParser(); parser.setProperty("http://xml.org/sax/features/external-general-entities", false); parser.setProperty("http://xml.org/sax/features/external-parameter-entities", false); parser.setProperty("http://apache.org/xml/features/disallow-doctype-decl", true); XMLReader xmlreader = parser.getXMLReader(); xmlreader.setContentHandler(indenter); xmlreader.parse(source); }
private Source getSource(final InputStream is, final Resource resource) throws ParserConfigurationException, SAXException { // todo: is this comment still relevant ?? // FIXME: need to use a SAXSource as the source for the transform // so we can plug in our own entity resolver Source src = null; if (entityResolver != null) { if (getFactory().getFeature(SAXSource.FEATURE)) { final SAXParserFactory spFactory = SAXParserFactory.newInstance(); spFactory.setNamespaceAware(true); final XMLReader reader = spFactory.newSAXParser().getXMLReader(); reader.setEntityResolver(entityResolver); src = new SAXSource(reader, new InputSource(is)); } else { throw new IllegalStateException("xcatalog specified, but " + "parser doesn't support SAX"); } } else { // WARN: Don't use the StreamSource(File) ctor. It won't work with // xalan prior to 2.2 because of systemid bugs. src = new StreamSource(is); } // The line below is a hack: the system id must an URI, but it is not // cleat to get the URI of an resource, so just set the name of the // resource as a system id src.setSystemId(resourceToURI(resource)); return src; }
@Override public void writeTo(Source source, Class<?> t, Type gt, Annotation[] as, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException { try { if (source instanceof StreamSource) { StreamSource stream = (StreamSource) source; InputSource inputStream = new InputSource(stream.getInputStream()); inputStream.setCharacterStream(inputStream.getCharacterStream()); inputStream.setPublicId(stream.getPublicId()); inputStream.setSystemId(source.getSystemId()); source = new SAXSource(saxParserFactory.get().newSAXParser().getXMLReader(), inputStream); } StreamResult sr = new StreamResult(entityStream); transformerFactory.get().newTransformer().transform(source, sr); } catch (SAXException ex) { throw new InternalServerErrorException(ex); } catch (ParserConfigurationException ex) { throw new InternalServerErrorException(ex); } catch (TransformerException ex) { throw new InternalServerErrorException(ex); } } }
javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware( true ); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); factory.newSAXParser(); reader=jaxpParser.getXMLReader(); throw new org.xml.sax.SAXException( ex ); } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { throw new org.xml.sax.SAXException( ex1.toString() ); } catch( NoSuchMethodError ex2 ) { try parent.setFeature("http://xml.org/sax/features/namespace-prefixes", true); throw new org.xml.sax.SAXException(XSLMessages.createMessage(XSLTErrorResources.ER_CANNOT_CALL_PARSE, null)); //"parse can not be called if the ContentHandler has not been set!"); getParent().parse(input); Exception e = m_transformer.getExceptionThrown(); if(null != e)
InputSource isource = SAXSource.sourceToInputSource(source); isource.setSystemId(baseID); XMLReader reader = null; reader = ((SAXSource) source).getXMLReader(); javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true); javax.xml.parsers.SAXParser jaxpParser = factory.newSAXParser(); reader = jaxpParser.getXMLReader(); throw new org.xml.sax.SAXException(ex); throw new org.xml.sax.SAXException(ex1.toString()); reader.setContentHandler(builder); reader.parse(isource); throw new TransformerConfigurationException(se.getMessage(), se);
? ((SAXSource) inputSource).getXMLReader() : null; javax.xml.parsers.SAXParserFactory.newInstance(); factory.setNamespaceAware( true ); javax.xml.parsers.SAXParser jaxpParser= factory.newSAXParser(); reader=jaxpParser.getXMLReader(); throw new org.xml.sax.SAXException( ex ); } catch( javax.xml.parsers.FactoryConfigurationError ex1 ) { throw new org.xml.sax.SAXException( ex1.toString() ); } catch( NoSuchMethodError ex2 ) { reader.setFeature("http://xml.org/sax/features/namespace-prefixes", true); throw new TransformerException(se.getMessage(), locator, se);
SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://xml.org/sax/features/external-general-entities", false); spf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); Source xmlSource = new SAXSource(spf.newSAXParser().getXMLReader(), new InputSource(reader)); Unmarshaller unmarshaller = JAXB_CONTEXT_MAP.get(clazz).createUnmarshaller(); return (T) unmarshaller.unmarshal(xmlSource);
try { HttpEntity entity = response.getEntity(); final InputStream in = entity.getContent(); final SAXParser parser = SAXParserFactory.newInstance().newSAXParser(); final XmlHandler handler = new XmlHandler(); Reader reader = new InputStreamReader(in, "UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); parser.parse(is, handler); //TODO: get the data from your handler } catch (final Exception e) { Log.e("ParseError", "Error parsing xml", e); }
public final List<BitextPatternRule> getRules(InputStream is, String filename) throws IOException { List<BitextPatternRule> rules; try { BitextPatternRuleHandler handler = new BitextPatternRuleHandler(); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxParser.parse(is, handler); rules = handler.getBitextRules(); return rules; } catch (Exception e) { throw new IOException("Cannot load or parse '" + filename + "'", e); } }
m_parserFactory = SAXParserFactory.newInstance(); m_parserFactory.setNamespaceAware(true); reader = m_parserFactory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException pce) { throw pce; // pass along pce reader.setFeature(NAMESPACES_FEATURE, true); reader.setFeature(NAMESPACE_PREFIXES_FEATURE, false); } catch (SAXException se) { throw new SAXException(ex); } catch (FactoryConfigurationError ex1) { throw new SAXException(ex1.toString()); } catch (NoSuchMethodError ex2) { } catch (AbstractMethodError ame) {