Skip to content
Related Articles

Related Articles

ParseContextClass in Java

View Discussion
Improve Article
Save Article
  • Difficulty Level : Easy
  • Last Updated : 26 Oct, 2021

ParseContext class is a component of the Java package org.apache.tika.parser, which is used to parse context and pass it on to the Tika  (The Apache Tika toolkit detects and extracts metadata and text from over a thousand different file types) parsers org.apache.tika.parser.ParseContext implements a Serializable interface.

public class ParseContext extends Object implements Serializable


1. ParseContext() – ParseContext() initialize a new instance of ParseContext class.

ParseContext p = new ParseContext()

Note: p is the new instance of ParseContext class.

Methods of ParseContext –




Return Type                                                


getDocumentBuilder() getDocumentBuilder() returns the DOM builder specified in this parsing context. DocumentBuilder


getSAXParser() getSAXParser() returns the SAX parser specified in this parsing context. SAXParser


getSAXParserFactory() getSAXParserFactory() returns the SAX parser factory specified in this parsing context. SAXParserFactory


getTransformer() getTransformer() returns the transformer specified in this parsing context. Transformer


 getXMLInputFactory()  getXMLInputFactory() returns the StAX input factory specified in this parsing context. XMLInputFactory


getXMLReader() getXMLReader() returns the XMLReader specified in this parsing context. XMLReader


get(Class<T> key)              get(Class<T> key) returns the object in this context that implements the given interface. <T> T


get(Class<T> key, T defaultValue) get(Class<T> key, T defaultValue)  returns the object in this context that implements the given interface. <T> T


 set(Class<T> key, T value)  set(Class<T> key, T value)  adds the given value to the context to implement the given interface. <T> void



// Java Program To Get Content of the
// document using Tika Toolkit and 
// ContextParser:
// importing File class
import org.apache.tika.exception.TikaException;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.txt.TXTParser;
import org.apache.tika.sax.BodyContentHandler;
// import the necessary Tika packages
import org.xml.sax.SAXException;
class GFG {
    public static void main(String[] args)
        // new instance of FIle is created
        File fileName = new File("tmp.txt");
        // new instance of FileInputStream is created for
        // reading purpose
        FileInputStream fileInputStream
            = new FileInputStream(fileName);
        // new instance of parseContext class is created
        ParseContext parseContext = new ParseContext();
        // new instance of MetaData is created
        MetaData metaData = new MetaData();
        // new instance of TXTParser is created for plain
        // text parsing purpose
        TXTParser textParser = new TXTParser();
        // new instance of BodyContentHandler is created
        BodyContentHandler bodyContentHandler
            = new BodyContentHandler();
        // TXTParser parse method is called for parsing a
        // document stream into sequence of XHTML SAX events.
                         bodyContentHandler, metaData,
        System.out.println("Contents of the document:"
                           + bodyContenthandler.toString());


Contents of the document:GFG is the best website for programmer

Note – The tmp.txt file contains the following data.

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!