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); } } } }); }
/** * Parse the content of the given {@link java.io.InputStream} * instance as XML using the specified * {@link org.xml.sax.helpers.DefaultHandler}. * * @param is InputStream containing the content to be parsed. * @param dh The SAX DefaultHandler to use. * * @throws IllegalArgumentException If the given InputStream is null. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(InputStream is, DefaultHandler dh) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputStream cannot be null"); } InputSource input = new InputSource(is); this.parse(input, dh); }
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 input = new InputSource(file.toURI().toASCIIString()); input.setByteStream(in); JAXP.newSAXParser().parse(input,new DefaultHandler() { private Locator loc; @Override
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; }
public Parse execute() { try { InputStream inputStream = streamSource.getInputStream(); parser.getSaxParserFactory().setFeature(XXE_PROCESSING, enableXxeProcessing); if (schemaResource == null) { // must be done before parser is created parser.getSaxParserFactory().setNamespaceAware(false); parser.getSaxParserFactory().setValidating(false); } SAXParser saxParser = parser.getSaxParser(); if (schemaResource != null) { saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaResource); } saxParser.parse(inputStream, new ParseHandler(this)); } catch (Exception e) { throw LOG.parsingFailureException(name, e); } return this; }
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; }
/** * Parse the content described by the giving Uniform Resource * Identifier (URI) as XML using the specified * {@link org.xml.sax.helpers.DefaultHandler}. * * @param uri The location of the content to be parsed. * @param dh The SAX DefaultHandler to use. * * @throws IllegalArgumentException If the uri is null. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(String uri, DefaultHandler dh) throws SAXException, IOException { if (uri == null) { throw new IllegalArgumentException("uri cannot be null"); } InputSource input = new InputSource(uri); this.parse(input, dh); }
/** * {@inheritDoc} */ @Override public ParseResult parse(RSyntaxDocument doc, String style) { result.clearNotices(); Element root = doc.getDefaultRootElement(); result.setParsedLines(0, root.getElementCount()-1); if (spf==null || doc.getLength()==0) { return result; } try { SAXParser sp = spf.newSAXParser(); Handler handler = new Handler(doc); DocumentReader r = new DocumentReader(doc); InputSource input = new InputSource(r); sp.parse(input, handler); r.close(); } catch (SAXParseException spe) { // A fatal parse error - ignore; a ParserNotice was already created. } catch (Exception e) { //e.printStackTrace(); // Will print if DTD specified and can't be found result.addNotice(new DefaultParserNotice(this, "Error parsing XML: " + e.getMessage(), 0, -1, -1)); } return result; }
public void parse( RepositoryElementReadListener repositoryElementReadListener ) throws Exception { this.repositoryElementReadListener = repositoryElementReadListener; SAXParserFactory factory = XMLParserFactoryProducer.createSecureSAXParserFactory(); this.saxParser = factory.newSAXParser(); this.saxParser.parse( new File( filename ), this ); }
public Parse execute() { try { InputStream inputStream = streamSource.getInputStream(); parser.getSaxParserFactory().setFeature(XXE_PROCESSING, enableXxeProcessing); if (schemaResource == null) { // must be done before parser is created parser.getSaxParserFactory().setNamespaceAware(false); parser.getSaxParserFactory().setValidating(false); } SAXParser saxParser = parser.getSaxParser(); if (schemaResource != null) { saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaResource); } saxParser.parse(inputStream, new ParseHandler(this)); } catch (Exception e) { throw LOG.parsingFailureException(name, e); } return this; }
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");
/** * <p>Parse the content of the given {@link java.io.InputStream} * instance as XML using the specified {@link org.xml.sax.HandlerBase}. * <i> Use of the DefaultHandler version of this method is recommended as * the HandlerBase class has been deprecated in SAX 2.0</i>.</p> * * @param is InputStream containing the content to be parsed. * @param hb The SAX HandlerBase to use. * * @throws IllegalArgumentException If the given InputStream is null. * @throws SAXException If parse produces a SAX error. * @throws IOException If an IO error occurs interacting with the * <code>InputStream</code>. * * @see org.xml.sax.DocumentHandler */ public void parse(InputStream is, HandlerBase hb) throws SAXException, IOException { if (is == null) { throw new IllegalArgumentException("InputStream cannot be null"); } InputSource input = new InputSource(is); this.parse(input, hb); }
/** * Checks an xml string is well formed. * * @param is * inputstream * @return true if the xml is well formed. */ public static boolean isXMLWellFormed( InputStream is ) throws KettleException { boolean retval = false; try { SAXParserFactory factory = XMLParserFactoryProducer.createSecureSAXParserFactory(); XMLTreeHandler handler = new XMLTreeHandler(); // Parse the input. SAXParser saxParser = factory.newSAXParser(); saxParser.parse( is, handler ); retval = true; } catch ( Exception e ) { throw new KettleException( e ); } return retval; }
String targetNamespace, String schemaLocation) { InputSource in = new InputSource(xml); SAXParserFactory sf = SAXParserFactory.newInstance(); sf.setNamespaceAware(true); sf.setValidating(true); SAXParser parser = sf.newSAXParser(); parser.setProperty( "http://java.sun.com/xml/jaxp/properties/schemaLanguage", parser.parse(in, errorHandler);
public void parse(File wordpressXml) { try { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); parser.parse(wordpressXml, this); } catch (Exception e) { log.warn("ConfigParser parser exception", e); } }
/** * Parse the content described by the giving Uniform Resource * Identifier (URI) as XML using the specified * {@link org.xml.sax.HandlerBase}. * <i> Use of the DefaultHandler version of this method is recommended as * the <code>HandlerBase</code> class has been deprecated in SAX 2.0</i> * * @param uri The location of the content to be parsed. * @param hb The SAX HandlerBase to use. * * @throws IllegalArgumentException If the uri is null. * @throws IOException If any IO errors occur. * @throws SAXException If any SAX errors occur during processing. * * @see org.xml.sax.DocumentHandler */ public void parse(String uri, HandlerBase hb) throws SAXException, IOException { if (uri == null) { throw new IllegalArgumentException("uri cannot be null"); } InputSource input = new InputSource(uri); this.parse(input, hb); }