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 void load(InputSource inputSource) throws ParserConfigurationException, SAXException, IOException { SAXParser saxParser = saxParserFactory.newSAXParser(); saxParser.parse(inputSource, new DefaultHandler() { @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if (localName.equals("factory")) { String className = attributes.getValue("class"); try { addClass(className); } catch (ClassNotFoundException e) { throw new SAXException("Cannot find Matcher class : " + className); } } } }); }
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); }
/** * 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 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); } }
InputSource input = new InputSource(file.toURI().toASCIIString()); input.setByteStream(in); JAXP.newSAXParser().parse(input,new DefaultHandler() { private Locator loc; @Override
InputSource requestSource = new InputSource(rawRequest); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); ParserAdapter adapter = new ParserAdapter(parser.getParser());
protected final SAXParser createParser(SAXParserFactory parserFactory) throws ParserConfigurationException, SAXException { parserFactory.setNamespaceAware(true); final SAXParser parser = parserFactory.newSAXParser(); final XMLReader reader = parser.getXMLReader(); //reader.setProperty("http://xml.org/sax/properties/lexical-handler", this); //$NON-NLS-1$ // disable DTD validation (bug 63625) try { // be sure validation is "off" or the feature to ignore DTD's will not apply reader.setFeature("http://xml.org/sax/features/validation", false); //$NON-NLS-1$ reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); //$NON-NLS-1$ } catch (SAXNotRecognizedException e) { // not a big deal if the parser does not recognize the features } catch (SAXNotSupportedException e) { // not a big deal if the parser does not support the features } return parser; }
public TransformXML() { try { saxParser = SAXParserFactory.newInstance().newSAXParser(); } catch (Exception e) { log.info("Error configuring XML parser: " + e); throw new RuntimeException(e); } }
public static XMLReader createSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler) throws SAXException, ParserConfigurationException { if (saxParserFactory == null) { throw new IllegalArgumentException("The provided SAX parser factory cannot be null"); } if (contentHandler == null) { throw new IllegalArgumentException("The provided SAX content handler cannot be null"); } saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false); saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); SAXParser saxParser = saxParserFactory.newSAXParser(); XMLReader xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(contentHandler); return xmlReader; } }
/** * Creates a new SAX XMLReader, with sensible defaults */ public static synchronized XMLReader newXMLReader() throws SAXException, ParserConfigurationException { XMLReader xmlReader = saxFactory.newSAXParser().getXMLReader(); xmlReader.setEntityResolver(IGNORING_ENTITY_RESOLVER); trySetSAXFeature(xmlReader, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySetXercesSecurityManager(xmlReader); return xmlReader; }
/** * 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; }
/** * Unmarshal a jaxb bean into a type of {@code resultClass} from the given {@code inputStream}. * * @param inputStream Input stream that contains input xml that should be processed. * @param saxParserFactory Sax parser factory for unmarshalling xml. * @param resultClass Class of the result bean into which the content of {@code inputStream} should be unmarshalled. * @param <T> Type of the result jaxb bean. * @return Unmarshalled jaxb bean. * * @throws JAXBException In case of jaxb problem. * @throws ParserConfigurationException In case of problem with parsing xml. * @throws SAXException In case of problem with parsing xml. */ public static <T> T unmarshall(InputStream inputStream, SAXParserFactory saxParserFactory, Class<T> resultClass) throws JAXBException, ParserConfigurationException, SAXException { JAXBContext jaxbContext = null; try { jaxbContext = JAXBContext.newInstance(resultClass); } catch (JAXBException ex) { throw new ProcessingException(LocalizationMessages.ERROR_WADL_JAXB_CONTEXT(), ex); } final SAXParser saxParser = saxParserFactory.newSAXParser(); SAXSource source = new SAXSource(saxParser.getXMLReader(), new InputSource(inputStream)); final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); final Object result = unmarshaller.unmarshal(source); return resultClass.cast(result); }
/** * 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); } }
SAXParserFactory spf = SAXParserFactoryImpl.newInstance(); spf.setNamespaceAware(true); spf.setValidating(false); spf.setFeature("http://xml.org/sax/features/validation", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); SAXParser sp = spf.newSAXParser() ; Source src = new SAXSource ( sp.getXMLReader(), new InputSource( input.getAbsolutePath() ) ) ; String resultFileName = input.getAbsolutePath().replaceAll(".xml$", ".cooked.xml" ) ; Result result = new StreamResult( new File (resultFileName) ) ; TransformerFactory tf = TransformerFactory.newInstance(); Source xsltSource = new StreamSource( new File ( COOKER_XSL ) ); xsl = tf.newTransformer( xsltSource ) ; xsl.setParameter( "srcDocumentName", input.getName() ) ; xsl.setParameter( "srcDocumentPath", input.getAbsolutePath() ) ; xsl.transform(src, result );