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) { }
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")));
factory.setValidating(true); factory.setNamespaceAware(false); try { factory.newSAXParser(); } catch (Exception pce_val) { validating = false; factory.setValidating(false); factory.setNamespaceAware(true); try { factory.newSAXParser(); } catch (Exception pce_nsa) { namespaceaware = false; factory.setValidating(validating); factory.setNamespaceAware(namespaceaware);
/** * Return the SAXParserFactory we will use, creating one if necessary. */ public SAXParserFactory getFactory() { if (factory == null) { factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(namespaceAware); factory.setXIncludeAware(xincludeAware); factory.setValidating(validating); factory.setSchema(schema); } return (factory); }
/** * Obtains a configured XMLReader. * * This method is used when the client-specified * {@link SAXSource} object doesn't have XMLReader. * * {@link Unmarshaller} is not re-entrant, so we will * only use one instance of XMLReader. * * Overriden in order to fix potential security issue. */ @Override protected XMLReader getXMLReader() throws JAXBException { if (reader == null) { try { SAXParserFactory parserFactory = XmlFactory.createParserFactory(context.disableSecurityProcessing); // there is no point in asking a validation because // there is no guarantee that the document will come with // a proper schemaLocation. parserFactory.setValidating(false); reader = parserFactory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { throw new JAXBException(e); } catch (SAXException e) { throw new JAXBException(e); } } return reader; }
((SAXParserFactory) factory).setValidating(((Boolean) sref.getProperty(PARSER_VALIDATING)).booleanValue()); ((SAXParserFactory) factory).setNamespaceAware(((Boolean) sref.getProperty(PARSER_NAMESPACEAWARE)).booleanValue()); } else { if (factory instanceof DocumentBuilderFactory) {
SAXParserFactory saxfac = SAXParserFactory.newInstance(); saxfac.setValidating(false); try { saxfac.setFeature("http://xml.org/sax/features/validation", false); saxfac.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); saxfac.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); saxfac.setFeature("http://xml.org/sax/features/external-general-entities", false); saxfac.setFeature("http://xml.org/sax/features/external-parameter-entities", false); } catch (Exception e1) { e1.printStackTrace(); }
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(); }
/** * Converts data from the given <code>InputStream</code> into HTML that is written to the given * <code>PrintWriter</code> */ private static void convert(InputStream in, PrintWriter out) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); SAXParser parser = factory.newSAXParser(); DefaultHandler handler = new GenerateMBeanHTML(out); parser.parse(in, handler); }
SAXParserFactory fac = SAXParserFactory.newInstance(); boolean val = false; fac.setNamespaceAware(true); switch (validate) { case 0: fac.setValidating(false); break; case 1: fac.setValidating(true); val = true; break; case 2: fac.setValidating(false); try { SchemaFactory sfac = SchemaFactory.
/** * Obtains a configured XMLReader. * * This method is used when the client-specified * {@link SAXSource} object doesn't have XMLReader. * * {@link Unmarshaller} is not re-entrant, so we will * only use one instance of XMLReader. * * Overriden in order to fix potential security issue. */ @Override protected XMLReader getXMLReader() throws JAXBException { if (reader == null) { try { SAXParserFactory parserFactory = XmlFactory.createParserFactory(context.disableSecurityProcessing); // there is no point in asking a validation because // there is no guarantee that the document will come with // a proper schemaLocation. parserFactory.setValidating(false); reader = parserFactory.newSAXParser().getXMLReader(); } catch (ParserConfigurationException e) { throw new JAXBException(e); } catch (SAXException e) { throw new JAXBException(e); } } return reader; }
factory.setValidating(true); factory.setNamespaceAware(false); try { factory.newSAXParser(); factory.setValidating(false); factory.setNamespaceAware(true); try { factory.newSAXParser(); factory.setValidating(validating); factory.setNamespaceAware(namespaceaware);
private static SAXParserFactory createParserFactory() throws ParserConfigurationException { if (saxParserFactory == null) { saxParserFactory = FactorySupport.createSaxParserFactory(); saxParserFactory.setNamespaceAware(true); saxParserFactory.setValidating(false);
/** * Create a reader if the use of the class did not specify another one. * The reason to not use {@link org.apache.tools.ant.util.JAXPUtils#getXMLReader()} was to * create our own factory with our own options. * @return a default XML parser */ @Override protected XMLReader createDefaultReader() { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setValidating(true); factory.setNamespaceAware(true); XMLReader reader = null; try { SAXParser saxParser = factory.newSAXParser(); reader = saxParser.getXMLReader(); } catch (ParserConfigurationException | SAXException e) { throw new BuildException(ERROR_PARSER_CREATION_FAILURE, e); } return reader; }
/** * Returns a SAXParser that may be used to process the XML descriptors. * * @return Parser which may be used to process the EJB descriptors. * @throws BuildException If the parser cannot be created or configured. */ private SAXParser getParser() throws BuildException { try { SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setValidating(true); return saxParserFactory.newSAXParser(); } catch (SAXException | ParserConfigurationException e) { throw new BuildException("Unable to create a SAXParser: " + e.getMessage(), e, getLocation()); } }
SAXParserFactory factory = get(SAXParserFactory.class); if (factory == null) { factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); try { factory.setFeature(
/** * XMLReader instances from this class will be configured to validate using * the supplied Schema instance. * * @param fac * The SAXParserFactory to use for creating XMLReader instances. * @param schema * The Schema to use for validation. */ public AbstractReaderSchemaFactory(final SAXParserFactory fac, final Schema schema) { if (schema == null) { throw new NullPointerException("Cannot create a " + "SchemaXMLReaderFactory with a null schema"); } saxfac = fac; if (saxfac != null) { saxfac.setNamespaceAware(true); saxfac.setValidating(false); saxfac.setSchema(schema); } }
/** * Creates a new instance. * @param publicIdToResourceNameMap maps public IDs to DTD resource names * @throws SAXException if an error occurs * @throws ParserConfigurationException if an error occurs */ protected XmlLoader(Map<String, String> publicIdToResourceNameMap) throws SAXException, ParserConfigurationException { this.publicIdToResourceNameMap = new HashMap<>(publicIdToResourceNameMap); final SAXParserFactory factory = SAXParserFactory.newInstance(); FeaturesForVerySecureJavaInstallations.addFeaturesForVerySecureJavaInstallations(factory); factory.setValidating(true); parser = factory.newSAXParser().getXMLReader(); parser.setContentHandler(this); parser.setEntityResolver(this); parser.setErrorHandler(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()); }
static SAXParserFactory saxFactory(XmlOptionsBean options) { SAXParserFactory saxFactory = SAXParserFactory.newInstance(); saxFactory.setValidating(false); saxFactory.setNamespaceAware(true); trySetSAXFeature(saxFactory, XMLConstants.FEATURE_SECURE_PROCESSING, true); trySetSAXFeature(saxFactory, XMLBeansConstants.FEATURE_LOAD_DTD_GRAMMAR, options.isLoadDTDGrammar()); trySetSAXFeature(saxFactory, XMLBeansConstants.FEATURE_LOAD_EXTERNAL_DTD, options.isLoadExternalDTD()); return saxFactory; }