List of usage examples for javax.xml.parsers SAXParserFactory newInstance
public static SAXParserFactory newInstance()
From source file:Main.java
/** * To get a SAXParserFactory instance which is then used to obtain an {@link SAXParser} instance. * /* w ww.j a v a 2s .co m*/ * @param namespaces Whether to provide namespace support. * @param validation Whether to produce a validating SAX parser. * @return the SAXParserFactory for further use */ public static SAXParserFactory getSAXParserFactory(final boolean validation, final boolean namespaces) { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(namespaces); factory.setValidating(validation); return factory; }
From source file:Main.java
public static void saxParserValidation(InputStream streamDaValidare, InputStream inputSchema, DefaultHandler handler) throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);/*from www . j a v a2 s .c om*/ if (inputSchema != null) { factory.setValidating(true); } else { factory.setValidating(false); } SAXParser parser = factory.newSAXParser(); if (inputSchema != null) { parser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA); parser.setProperty(JAXP_SCHEMA_SOURCE, inputSchema); } parser.parse(streamDaValidare, handler); }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXMLFromString(Class<?> class1, String content) throws JAXBException, FileNotFoundException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (StringReader reader = new StringReader(content)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }/* w ww.j a v a 2 s. com*/ }
From source file:Main.java
public static SAXParserFactory createParserFactory() { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(true);/*from www .j a va 2 s . c o m*/ factory.setValidating(false); try { factory.setFeature("http://xml.org/sax/features/external-general-entities", false); factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false); } catch (ParserConfigurationException ex) { } catch (SAXNotRecognizedException ex) { } catch (SAXNotSupportedException ex) { } try { factory.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false); factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); } catch (ParserConfigurationException ex) { } catch (SAXNotRecognizedException ex) { } catch (SAXNotSupportedException ex) { } return factory; }
From source file:Main.java
public static void parseDocumentByString(String result, DefaultHandler defaultHandler) { try {//from w ww. java 2 s. c o m SAXParserFactory sf = SAXParserFactory.newInstance(); SAXParser sp = sf.newSAXParser(); sp.parse(new ByteArrayInputStream(result.getBytes("UTF-8")), defaultHandler); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:Main.java
@SuppressWarnings("unchecked") public static <T> T readXML(Class<?> class1, File file) throws JAXBException, IOException, SAXException, ParserConfigurationException { JAXBContext context = JAXBContext.newInstance(class1); Unmarshaller um = context.createUnmarshaller(); SAXParserFactory spf = SAXParserFactory.newInstance(); spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); spf.setFeature("http://xml.org/sax/features/validation", false); XMLReader xr = (XMLReader) spf.newSAXParser().getXMLReader(); try (FileReader reader = new FileReader(file)) { SAXSource source = new SAXSource(xr, new InputSource(reader)); T obj = (T) um.unmarshal(source); return obj; }//from w ww. j a va2s. co m }
From source file:Main.java
public static SAXParser saxParser() { try {//from w ww .ja v a 2 s.co m if (saxParserFactory == null) { saxParserFactory = SAXParserFactory.newInstance(); saxParserFactory.setNamespaceAware(true); saxParserFactory.setValidating(false); } return saxParserFactory.newSAXParser(); } catch (ParserConfigurationException e) { throw new RuntimeException("Error configuring SAX parser factory", e); } catch (SAXException e) { throw new RuntimeException("Error configuring SAX parser factory", e); } }
From source file:Main.java
/** * Get an instance of an XML reader from the XMLReaderFactory. * * @return the XMLReader.//from w ww . j ava2 s . c o m */ public static XMLReader getXmlReader() { try { final XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader(); reader.setFeature("http://xml.org/sax/features/namespaces", true); reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false); reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); return reader; } catch (final Exception e) { throw new RuntimeException("Unable to create XMLReader", e); } }
From source file:Main.java
public static Set<String> compareXmlFiles(String firstFolderPath, String secondFolderPath, boolean outConsole) { Set<String> identicalFiles = new HashSet<String>(); File firstFolder = new File(firstFolderPath); File secondFolder = new File(secondFolderPath); Map<String, File> firstFileMap = new HashMap<String, File>(); Map<String, File> secondFileMap = new HashMap<String, File>(); traverseFolder(firstFileMap, firstFolder, firstFolder); traverseFolder(secondFileMap, secondFolder, secondFolder); // temporarily - comparison by MD5 instead of XML parsing SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = null;/*from w w w.j a va2 s .co m*/ try { saxParser = factory.newSAXParser(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } System.out.println("Parser: " + saxParser); Set<String> sourceKeys = firstFileMap.keySet(); Set<String> destKeys = secondFileMap.keySet(); for (String key1 : sourceKeys) { File file1 = firstFileMap.get(key1); File file2 = secondFileMap.get(key1); if (file1 != null && file2 != null) { try { String node1 = calculateMd5Checksum(file1.getCanonicalPath()); String node2 = calculateMd5Checksum(file2.getCanonicalPath()); // System.out.println("Source:" + node1 + " Dest:" + node2); if (node1.equals(node2)) { firstFileMap.remove(key1); secondFileMap.remove(key1); } } catch (Exception ex) { ex.printStackTrace(); // can be ignored } } } for (String key1 : sourceKeys) { if (destKeys.contains(key1)) { identicalFiles.add(key1); sourceKeys.remove(key1); destKeys.remove(key1); } } if (outConsole == true) { } return identicalFiles; }
From source file:Main.java
/** * Creates a non-validating, non-namespace-aware {@link XMLReader} using the specified * {@link ContentHandler}.// w ww . j a v a 2 s.c o m * * <p>If the given {@link ContentHandler} is <code>null</code>, the {@link XMLReader} is * not initialised. * * @param handler The content handler to use. * * @return The requested {@link XMLReader} * * @throws SAXException Should a SAX exception occur * @throws ParserConfigurationException Should a parser config exception occur */ public static XMLReader makeXMLReader(ContentHandler handler) throws SAXException, ParserConfigurationException { SAXParserFactory factory = SAXParserFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); XMLReader reader = factory.newSAXParser().getXMLReader(); if (handler != null) { reader.setContentHandler(handler); } return reader; }