DocumentBuilderFactory setIgnoringElementContentWhitespace() Method



Description

The Javax.xml.parsers.DocumentBuilderFactory.setIgnoringElementContentWhitespace(boolean whitespace) method specifies that the parsers created by this factory must eliminate whitespace in element content (sometimes known loosely as 'ignorable whitespace') when parsing XML documents (see XML Rec 2.10). Note that only whitespace which is directly contained within element content that has an element only content model (see XML Rec 3.2.1) will be eliminated. Due to reliance on the content model this setting requires the parser to be in validating mode. By default the value of this is set to false.

Declaration

Following is the declaration for Javax.xml.parsers.DocumentBuilderFactory.setIgnoringElementContentWhitespace() method

public void setIgnoringElementContentWhitespace(boolean whitespace)

Parameters

whitespace − true if the parser created must eliminate whitespace in the element content when parsing XML documents; false otherwise.

Return Value

This method does not return a value.

Exception

NA

Example

The following example shows the usage of Javax.xml.parsers.DocumentBuilderFactory.setIgnoringElementContentWhitespace() method.

package com.tutorialspoint;

import javax.xml.parsers.DocumentBuilderFactory;

public class DocumentBuilderFactoryDemo {

   public static void main(String[] args) {

      // create a new DocumentBuilderFactory
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

      // change this configuration
      factory.setIgnoringElementContentWhitespace(true);

      // check if factory is ignoring whitespaces in elements
      System.out.println("" + factory.isIgnoringElementContentWhitespace());


   }
}

If we compile the code and execute it, this will produce the following result −

true
javax_xml_parsers_documentbuilderfactory.htm
Advertisements