Javax.xml.parsers.DocumentBuilder.reset() Method



Description

The Javax.xml.parsers.DocumentBuilder.reset() method resets this DocumentBuilder to its original configuration.

DocumentBuilder is reset to the same state as when it was created with DocumentBuilderFactory.newDocumentBuilder(). reset() is designed to allow the reuse of existing DocumentBuilders thus saving resources associated with the creation of new DocumentBuilders.

The reset DocumentBuilder is not guaranteed to have the same EntityResolver or ErrorHandler Objects, e.g. Object.equals(Object obj). It is guaranteed to have a functionally equal EntityResolver and ErrorHandler.

Declaration

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

public void reset()

Parameters

NA

Return Value

This method does not return a value.

Exception

UnsupportedOperationException − When implementation does not override this method.

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.reset() method.

package com.tutorialspoint;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

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();

         // create a new document from input stream and an empty systemId
         Document doc = builder.parse("Student.xml");

         // get the first element
         Element element = doc.getDocumentElement();

         // get all child nodes
         NodeList nodes = element.getChildNodes();

         // print the text content of each child
         for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println("" + nodes.item(i).getTextContent());
         }

         // reset the builder
         builder.reset();
         System.out.println("Builder is reseted and ready to parse again.");

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


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


   
12

   
Malik


Builder is reseted and ready to parse again.
javax_xml_parsers_documentbuilder.htm
Advertisements