Java Utililty Methods XML SAX Parser

List of utility methods to do XML SAX Parser

Description

The list of methods to do XML SAX Parser are organized into topic(s).

Method

voidaddMessagesToMap(final Map hangmanMap, String messagesURIString)
add Messages To Map
DefaultHandler handler = new DefaultHandler() {
    String name;
    String value;
    public void startElement(String uri, String localName, String qName, Attributes attributes)
            throws SAXException {
        name = attributes.getValue("name");
    public void characters(char[] ch, int start, int length) throws SAXException {
...
booleancanDisableExternalDtds(SAXParserFactory parserFactory)
can Disable External Dtds
if (canDisableExternalDtds == null) {
    try {
        parserFactory.getFeature(XERCES_LOAD_EXTERNAL_DTD);
        canDisableExternalDtds = Boolean.TRUE;
    } catch (Exception ex) {
        canDisableExternalDtds = Boolean.FALSE;
return canDisableExternalDtds.booleanValue();
SAXParserFactorycreateNamespaceAwareSAXParserFactory()
Creates a namespace-aware SAXParserFactory
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setNamespaceAware(true);
return spf;
XMLReadercreateSafeSaxReader(SAXParserFactory saxParserFactory, ContentHandler contentHandler)
create Safe Sax Reader
if (saxParserFactory == null) {
    throw new IllegalArgumentException("The provided SAX parser factory cannot be null");
if (contentHandler == null) {
    throw new IllegalArgumentException("The provided SAX content handler cannot be null");
saxParserFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
saxParserFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
...
SAXParsercreateSaxParser()
Create a new SAXParser which processes XML securely.
SAXParserFactory spf = SAXParserFactory.newInstance();
try {
    spf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
    return spf.newSAXParser();
} catch (ParserConfigurationException | SAXException e) {
    throw new IllegalStateException(e);
SAXParsercreateSAXParser()
Creates SAX parser and disables XXE
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); 
SAXParser parser = factory.newSAXParser();
return parser;
SAXParsercreateSAXParser(DefaultHandler dh)
create SAX Parser
try {
    SAXParser sp = spf.newSAXParser();
    if (dh instanceof LexicalHandler)
        sp.setProperty("http://xml.org/sax/properties/lexical-handler", dh);
    return sp;
} catch (ParserConfigurationException e) {
    throw new SAXException(e);
SAXParserFactorycreateSaxParserFactory()
create Sax Parser Factory
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setValidating(true);
saxParserFactory.setNamespaceAware(true);
return saxParserFactory;
SchemacreateSchema(String url)
create Schema
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
try {
    return factory.newSchema(new URL(url));
} catch (MalformedURLException | SAXException e) {
    throw new RuntimeException(e);
XMLReadercreateXMLReader()
Create a simple parser.
return createXMLReader(false, false);