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.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) { }
public TransformXML() { try { saxParser = SAXParserFactory.newInstance().newSAXParser(); } catch (Exception e) { log.info("Error configuring XML parser: " + e); throw new RuntimeException(e); } }
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); }
InputSource requestSource = new InputSource(rawRequest); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); ParserAdapter adapter = new ParserAdapter(parser.getParser());
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(); }
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); } }
public XmlConfigurator(SugarConfiguration sugarConfiguration, ClassLoader classLoader) { this.sugarConfiguration = sugarConfiguration; this.classLoader = classLoader; saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); qdox = new QDox(); }
public Set<String> parseContents(InputSource contents) throws IOException, ParserConfigurationException, SAXException { namespaces.clear(); // Parse the file into we have what we need (or an error occurs). if (factory == null) { factory = SAXParserFactory.newInstance(); } if (factory != null) { SAXParser parser = createParser(factory); // to support external entities specified as relative URIs (see bug 63298) contents.setSystemId("/"); //$NON-NLS-1$ parser.parse(contents, this); } return namespaces; }
/** * Creates a new <code>LoadXMLAction</code> instance. * * @param aParent the parent frame * @param aModel the model to add events to * @exception SAXException if an error occurs * @throws ParserConfigurationException if an error occurs */ LoadXMLAction(JFrame aParent, MyTableModel aModel) throws SAXException, ParserConfigurationException { mParent = aParent; mHandler = new XMLFileHandler(aModel); mParser = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); mParser.setContentHandler(mHandler); }
private void validateInternal(String xml, String dtdPath, String docType) throws SAXException, IOException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); SAXParser saxParser = factory.newSAXParser(); //used for removing existing DOCTYPE from grammar.xml files String cleanXml = xml.replaceAll("<!DOCTYPE.+>", ""); String decl = "<?xml version=\"1.0\""; String endDecl = "?>"; URL dtdUrl = this.getClass().getResource(dtdPath); if (dtdUrl == null) { throw new RuntimeException("DTD not found in classpath: " + dtdPath); } String dtd = "<!DOCTYPE " + docType + " PUBLIC \"-//W3C//DTD Rules 0.1//EN\" \"" + dtdUrl + "\">"; int pos = cleanXml.indexOf(decl); int endPos = cleanXml.indexOf(endDecl); if (pos == -1) { throw new IOException("No XML declaration found in '" + cleanXml.substring(0, Math.min(100, cleanXml.length())) + "...'"); } String newXML = cleanXml.substring(0, endPos+endDecl.length()) + "\r\n" + dtd + cleanXml.substring(endPos+endDecl.length()); InputSource is = new InputSource(new StringReader(newXML)); saxParser.parse(is, new ErrorHandler()); }
/** Creates a new instance of BinaryInputArchive */ public XmlInputArchive(InputStream in) throws ParserConfigurationException, SAXException, IOException { valList = new ArrayList<Value>(); DefaultHandler handler = new XMLParser(valList); SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); SAXParser parser = factory.newSAXParser(); parser.parse(in, handler); vLen = valList.size(); vIdx = 0; }
javax.xml.parsers.SAXParserFactory factory = javax.xml.parsers.SAXParserFactory.newInstance(); factory.setValidating(false); javax.xml.parsers.SAXParser parser = factory.newSAXParser(); parser.parse(new java.io.File("x.xml"), new org.xml.sax.helpers.DefaultHandler(){ public org.xml.sax.InputSource resolveEntity(String publicId, String systemId) throws org.xml.sax.SAXException, java.io.IOException { System.out.println("Ignoring: " + publicId + ", " + systemId); return new org.xml.sax.InputSource(new java.io.StringReader("")); } });
/** * Setups the XML parser with validation. * * @throws Exception * any error */ @BeforeClass public static void init() throws Exception { saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setValidating(true); saxParserFactory.setNamespaceAware(true); // Hope we're using Xerces, or this may not work! // Note: Features are listed here // http://xerces.apache.org/xerces2-j/features.html saxParserFactory.setFeature("http://xml.org/sax/features/validation", true); saxParserFactory.setFeature("http://apache.org/xml/features/validation/schema", true); saxParserFactory.setFeature("http://apache.org/xml/features/validation/schema-full-checking", true); validateDefaultHandler = new ValidateDefaultHandler(); saxParser = saxParserFactory.newSAXParser(); }
/** * @param is stream with the XML rules * @param filename used only for verbose exception message - should refer to where the stream comes from */ public final List<AbstractPatternRule> getRules(InputStream is, String filename) throws IOException { try { PatternRuleHandler handler = new PatternRuleHandler(filename); handler.setRelaxedMode(relaxedMode); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); Tools.setPasswordAuthenticator(); saxParser.getXMLReader().setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxParser.parse(is, handler); return handler.getRules(); } catch (Exception e) { throw new IOException("Cannot load or parse input stream of '" + filename + "'", e); } }
/** * Initialize the SAX parser instance. * @throws HistoryException */ private void initSaxParser() throws HistoryException { SAXParserFactory factory = SAXParserFactory.newInstance(); saxParser = null; try { saxParser = factory.newSAXParser(); } catch (Exception ex) { throw new HistoryException("Failed to create SAX parser", ex); } }
@Override public SAXParserFactory get() { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); if (!isXmlSecurityDisabled()) { factory = new SecureSaxParserFactory(factory); } return factory; } }
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")));
public DocumentData parseHTMLDocument(String document) throws HTMLParseException { try { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(false); saxParserFactory.setValidating(false); saxParserFactory.setXIncludeAware(false); SAXParser saxParser = saxParserFactory.newSAXParser(); HTMLDocumentHandler dh = new HTMLDocumentHandler(htmlDocumentBuilderFactory); saxParser.parse(new ByteArrayInputStream(document.getBytes("UTF-8")), dh); return dh.getDocument(); } catch (ParserConfigurationException | SAXException | IOException exp) { throw new HTMLParseException(exp); } } }
final Map<String,VersionNumber> requestedPlugins = new TreeMap<String,VersionNumber>(); try { SAXParserFactory.newInstance().newSAXParser().parse(configXml, new DefaultHandler() { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { String plugin = attributes.getValue("plugin");