DocumentBuilder isNamespaceAware() Method



Description

The Javax.xml.parsers.DocumentBuilder.isNamespaceAware() method indicates whether or not this parser is configured to understand namespaces.

Declaration

Following is the declaration for Javax.xml.parsers.DocumentBuilder.isNamespaceAware() method

public abstract boolean isNamespaceAware()

Parameters

NA

Return Value

This method returns true if this parser is configured to understand namespaces; false otherwise.

Exception

NA

Example

For our examples to work, a xml file named Student.xml is needed in our CLASSPATH. The contents of this XML are the following −

<?xml version = "1.0" encoding = "UTF-8" standalone = "yes"?>
<student id = "10">
   <age>12</age>
   <name>Malik</name>
</student>

The following example shows the usage of Javax.xml.parsers.DocumentBuilder.isNamespaceAware() method.

package com.tutorialspoint;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

public class DocumentBuilderDemo {

   public static void main(String[] args) {

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

      try {
         // use the factory to create a documentbuilder
         DocumentBuilder builder = factory.newDocumentBuilder();

         // check if builder is name space aware
         System.out.println("" + builder.isNamespaceAware());

         // use the factory again to create another documentbuilder
         factory.setNamespaceAware(true);
         DocumentBuilder builder2 = factory.newDocumentBuilder();

         // check if builder2 is name space aware
         System.out.println("" + builder2.isNamespaceAware());

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

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

false
true
javax_xml_parsers_documentbuilder.htm
Advertisements