Example usage for javax.xml.parsers SAXParserFactory newInstance

List of usage examples for javax.xml.parsers SAXParserFactory newInstance

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParserFactory newInstance.

Prototype


public static SAXParserFactory newInstance() 

Source Link

Document

Obtain a new instance of a SAXParserFactory .

Usage

From source file:TrySAX.java

private void process(File file) {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*from w w w.java2 s  .c o m*/
    spf.setValidating(true);
    System.out.println("Parser will " + (spf.isNamespaceAware() ? "" : "not ") + "be namespace aware");
    System.out.println("Parser will " + (spf.isValidating() ? "" : "not ") + "validate XML");
    try {
        parser = spf.newSAXParser();
        System.out.println("Parser object is: " + parser);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    } catch (ParserConfigurationException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    System.out.println("\nStarting parsing of " + file + "\n");
    try {
        parser.parse(file, this);
    } catch (IOException e) {
        e.printStackTrace(System.err);
    } catch (SAXException e) {
        e.printStackTrace(System.err);
    }
}

From source file:VSX.java

public TreeModel parse(String filename) {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    XMLTreeHandler handler = new XMLTreeHandler();
    try {//from ww w  .  j  av a2s  .  c  o m
        // Parse the input.
        SAXParser saxParser = factory.newSAXParser();
        saxParser.parse(new File(filename), handler);
    } catch (Exception e) {
        System.err.println("File Read Error: " + e);
        e.printStackTrace();
        return new DefaultTreeModel(new DefaultMutableTreeNode("error"));
    }
    return new DefaultTreeModel(handler.getRoot());
}

From source file:org.apache.clerezza.commons.rdf.impl.sparql.SparqlResultParser.java

static Object parse(InputStream in) throws IOException {
    try {//from   ww  w  . j  a  va  2 s  . c  o  m
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
        xmlReader.setContentHandler(sparqlsResultsHandler);
        xmlReader.parse(new InputSource(in));
        return sparqlsResultsHandler.getResults();
    } catch (ParserConfigurationException | SAXException ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

/** Create a SAX parser from the JAXP factory.
 * The result can be used to parse XML files.
 * //  ww w.  j  av  a  2  s. c o m
 * <p>See class Javadoc for hints on setting an entity resolver.
 * This parser has its entity resolver set to the system entity resolver chain.
 *
 * @param validate if true, a validating parser is returned
 * @param namespaceAware if true, a namespace aware parser is returned
 *
 * @throws FactoryConfigurationError Application developers should never need to directly catch errors of this type.
 * @throws SAXException if a parser fulfilling given parameters can not be created
 *
 * @return XMLReader configured according to passed parameters
 */
public static XMLReader createXMLReader(boolean validate, boolean namespaceAware) throws SAXException {

    SAXParserFactory factory = SAXParserFactory.newInstance();

    factory.setValidating(validate);
    factory.setNamespaceAware(namespaceAware);

    try {
        return factory.newSAXParser().getXMLReader();
    } catch (ParserConfigurationException ex) {
        throw new SAXException("Cannot create parser satisfying configuration parameters", ex); //NOI18N                        
    }

}

From source file:com.truven.runtime.DataTablesParser.java

/**
 * Parses the tables./*from  w w  w .j av a  2s. c o  m*/
 * 
 * @param file
 *            the file
 * @return the hash map
 */
public static HashMap<String, DataTable> parseTables(final File file) {
    DataTablesParser dtp = new DataTablesParser();

    try {
        SAXParserFactory saxfactory = SAXParserFactory.newInstance();
        SAXParser saxParser = saxfactory.newSAXParser();
        saxParser.parse(file, dtp);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return dtp.getTables();

}

From source file:com.mindquarry.desktop.I18N.java

protected static Map<String, String> initTranslationMap(String fileBase, String fileSuffix) {
    try {//from ww  w  .  j  av a 2  s .  co  m
        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        parserFactory.setValidating(false);
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();
        TranslationMessageParser translationParser = new TranslationMessageParser();
        reader.setContentHandler(translationParser);
        reader.setErrorHandler(translationParser);
        // TODO: use "xx_YY" if available, use "xx" otherwise:
        String transFile = fileBase + Locale.getDefault().getLanguage() + fileSuffix;
        InputStream is = I18N.class.getResourceAsStream(transFile);
        if (is == null) {
            // no translation available for this language
            log.debug("No translation file available for language: " + Locale.getDefault().getLanguage());
            return new HashMap<String, String>();
        }
        log.debug("Loading translation file " + transFile + " from JAR");
        reader.parse(new InputSource(is));
        return translationParser.getMap();
    } catch (Exception e) {
        throw new RuntimeException(e.toString(), e);
    }
}

From source file:elaborate.util.XmlUtil.java

public static boolean isWellFormed(String body) {
    try {/*from   w ww.  j  a v  a  2s.co m*/
        SAXParser parser;
        parser = SAXParserFactory.newInstance().newSAXParser();
        DefaultHandler dh = new DefaultHandler();
        parser.parse(new InputSource(new StringReader(body)), dh);
    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
        return false;
    } catch (SAXException e1) {
        e1.printStackTrace();
        Log.error("body={}", body);
        return false;
    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

From source file:ebay.dts.client.CreateLMSParser.java

/**
 * Create the SAX parser//ww  w . j  a  v  a2 s . c  o  m
 */
private void create() {
    try {
        // Obtain a new instance of a SAXParserFactory.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        // Specifies that the parser produced by this code will provide support for XML namespaces.
        factory.setNamespaceAware(true);
        // Specifies that the parser produced by this code will validate documents as they are parsed.
        factory.setValidating(true);
        // Creates a new instance of a SAXParser using the currently configured factory parameters.
        saxParser = factory.newSAXParser();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

From source file:fedora.server.security.servletfilters.xmluserfile.ParserXmlUserfile.java

public ParserXmlUserfile(InputStream xmlStream) throws IOException {
    log.debug(this.getClass().getName() + ".init<> " + " begin");

    m_xmlStream = xmlStream;/*from www .j  ava  2s  .  c  o m*/
    try {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        log.debug(this.getClass().getName() + ".init<> " + " after newInstance");
        spf.setNamespaceAware(true);
        log.debug(this.getClass().getName() + ".init<> " + " after setNamespaceAware");
        m_parser = spf.newSAXParser();
        log.debug(this.getClass().getName() + ".init<> " + " after newSAXParser");
    } catch (Exception e) {
        e.printStackTrace();
        throw new IOException("Error getting XML parser: " + e.getMessage());
    } catch (Throwable t) {
        log.fatal(this.getClass().getName() + ".init<> " + " caught me throwable");
        t.printStackTrace();
        log.fatal(this.getClass().getName() + ".populateCacheElement() " + t);
        log.fatal(this.getClass().getName() + ".populateCacheElement() " + t.getMessage() + " "
                + (t.getCause() == null ? "" : t.getCause().getMessage()));
    }
}

From source file:Main.java

public static final XMLReader newXMLReader() throws SAXException, ParserConfigurationException {
    final SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setValidating(false);/*  w w  w.  ja v a2s . c o m*/
    spf.setNamespaceAware(true);
    return spf.newSAXParser().getXMLReader();
}