Example usage for javax.xml.parsers SAXParserFactory newSAXParser

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

Introduction

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

Prototype


public abstract SAXParser newSAXParser() throws ParserConfigurationException, SAXException;

Source Link

Document

Creates a new instance of a SAXParser using the currently configured factory parameters.

Usage

From source file:Main.java

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

From source file:Main.java

public static SAXParser getSAXParser() throws Exception {
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setValidating(true);
    saxParserFactory.setNamespaceAware(true);
    SAXParser saxParser = saxParserFactory.newSAXParser();
    saxParser.setProperty("http://java.sun.com/xml/jaxp/properties/schemaLanguage",
            "http://www.w3.org/2001/XMLSchema");
    return saxParser;
}

From source file:Main.java

/**
 * Creates a non-validating, non-namespace-aware {@link XMLReader} using the specified
 * {@link ContentHandler}./*from   ww  w  .  j av a 2s .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;
}

From source file:Main.java

/**
 * Returns a {@link SAXSource} for the provided {@link InputStream} that explicitly forfeit external DTD validation
 *
 * @param in the {@link InputStream} for the {@link SAXSource}
 * @return a {@link SAXSource} for the provided {@link InputStream} that explicitly forfeit external DTD validation
 * @throws SAXException                 if a SAX error occurs
 * @throws ParserConfigurationException if a configuration error occurs
 *//*  w  w  w  .  java  2  s.c  o  m*/
public static Source toNonValidatingSAXSource(InputStream in)
        throws SAXException, ParserConfigurationException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
    XMLReader xmlReader = spf.newSAXParser().getXMLReader();
    InputSource inputSource = new InputSource(in);
    return new SAXSource(xmlReader, inputSource);
}

From source file:Util.java

/**
 * Creates an XMLReader with default feature set. Note that all Cayenne internal XML
 * parsers should probably use XMLReader obtained via this method for consistency
 * sake, and can customize feature sets as needed.
 *//*  w  w  w.jav  a2s .  c  o  m*/
public static XMLReader createXmlReader() throws SAXException, ParserConfigurationException {
    SAXParserFactory spf = SAXParserFactory.newInstance();

    // Create a JAXP SAXParser
    SAXParser saxParser = spf.newSAXParser();

    // Get the encapsulated SAX XMLReader
    XMLReader reader = saxParser.getXMLReader();

    // set default features
    reader.setFeature("http://xml.org/sax/features/namespaces", true);

    return reader;
}

From source file:Main.java

/** Create a SAX parser from the JAXP factory.
 * The result can be used to parse XML files.
 * //from w w w  .j a v a  2s . c  om
 * <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:Main.java

private static XMLReader makeXMLReader() throws ParserConfigurationException, SAXException {
    final SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);//  w  ww.  j av  a  2s .  c om
    final XMLReader reader = factory.newSAXParser().getXMLReader();
    return reader;
}

From source file:Main.java

public static XMLReader xmlReader() throws ParserConfigurationException, SAXException {
    SAXParserFactory spf = SAX_PARSER_FACTORY.get();
    if (spf == null) {
        spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);//from w w  w  .  j  av  a 2  s . c om
        SAX_PARSER_FACTORY.set(spf);
    }
    return spf.newSAXParser().getXMLReader();
}

From source file:Main.java

public static SAXParser getParser() throws ParserConfigurationException, SAXException {
    SAXParserFactory factory = SAXParserFactory.newInstance();
    factory.setNamespaceAware(true);//from   w w  w  . ja v a2  s. co  m
    factory.setValidating(false);
    factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    return factory.newSAXParser();
}

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

/**
 * Parses the tables./*w  ww.  ja v  a 2 s .  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();

}