- ignorableWhitespace() Event Handler


How do I catch those whitespace text contents that are removed from the SAX parser?

✍: FYIcenter


When DTD is applied to the XML document, the SAX parser will quietly remove whitespace text contents and not call the characters() event handler.

If you want to catch those whitespace text contents during the parsing process, you need add the ignorableWhitespace() event handler as shown in this example,

// Copyright (c) 2017
import javax.xml.parsers.SAXParserFactory;
import javax.xml.parsers.SAXParser;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;

public class SaxXmlWhitespace extends DefaultHandler {
   static String dot = "............................................................";
   static int l = 0;
   static String show = "noshow";
   public static void main(String[] args) throws Exception {
      SAXParserFactory f = SAXParserFactory.newInstance();
      SAXParser p = f.newSAXParser();
      System.out.println("Parser class: "+p.getClass().getName());

      show = args[1];
    p.parse(new File(args[0]), new SaxXmlWhitespace());
   public void startElement(String uri, String lName, String qName, Attributes atts) {
   public void endElement(String uri, String lName, String qName) {
   public void characters(char[] ch, int start, int length) {
      System.out.print("("+(new String(ch,start,length))+")");
   public void ignorableWhitespace(char[] ch, int start, int length) {
      if (show.equals("show")) {
         System.out.print("["+(new String(ch,start,length))+"]");

Compile and run the example program,


>\fyicenter\jdk-1.8.0\bin\java SaxXmlWhitespace UserDTD.xml show

Parser class:

..Name(Frank Y. Ivy)[
..Sex(  Male)[

If you run it again with "noshow", you will not see those whitespace text contents in the output:

>\fyicenter\jdk-1.8.0\bin\java SaxXmlWhitespace UserDTD.xml noshow

Parser class:

..Name(Frank Y. Ivy)
..Sex(  Male) - SAX Parser with DTD Validation

SAX Parser for XML File with DTD

Using XML SAX API with Apache Xerces

⇑⇑ FAQ for Apache Xerces XML Parser

2017-12-09, 1734🔥, 0💬