Call isText property to check if text extraction is supported;
Call isToc property to check if table of contents extraction is supported;
Call getToc method and obtain collection of tables;
Iterate through the collection and get a text from tables.
The following example shows how to extract data from Sqlite database:
// Create DbConnection objectjava.sql.Connectionconnection=java.sql.DriverManager.getConnection(String.format("jdbc:sqlite:%s",Constants.SampleDatabase));// Create an instance of Parser class to extract tables from the databasetry(Parserparser=newParser(connection)){// Check if text extraction is supportedif(!parser.getFeatures().isText()){System.out.println("Text extraction isn't supported.");return;}// Check if toc extraction is supportedif(!parser.getFeatures().isToc()){System.out.println("Toc extraction isn't supported.");return;}// Get a list of tablesIterable<TocItem>toc=parser.getToc();// Iterate over tablesfor(TocItemi:toc){// Print the table nameSystem.out.println(i.extractText());// Extract a table content as a texttry(TextReaderreader=parser.getText(i.getPageIndex().intValue())){System.out.println(reader.readToEnd());}}}
Extract data with connection string
To create an instance of Parser class to extract data from a database with connection string the following constructor is used:
Parser(StringfilePath,LoadOptionsloadOptions);
The list of tables is represented as table of contents. The table extraction is processed by getText method.
Here are the steps to extract emails from Sqlite database:
Call isText property to check if text extraction is supported;
Call isToc property to check if table of contents extraction is supported;
Call getToc method and obtain collection of tables;
Iterate through the collection and get a text from tables.
The following example shows how to extract data from Sqlite database:
StringconnectionString=String.format("jdbc:sqlite:%s",Constants.SampleDatabase);// Create an instance of Parser class to extract tables from the database
// As filePath connection parameters are passed; LoadOptions is set to Database file format
try(Parserparser=newParser(connectionString,newLoadOptions(FileFormat.Database))){// Check if text extraction is supported
if(!parser.getFeatures().isText()){System.out.println("Text extraction isn't supported.");return;}// Check if toc extraction is supported
if(!parser.getFeatures().isToc()){System.out.println("Toc extraction isn't supported.");return;}// Get a list of tables
Iterable<TocItem>toc=parser.getToc();// Iterate over tables
for(TocItemi:toc){// Print the table name
System.out.println(i.getText());// Extract a table content as a text
try(TextReaderreader=parser.getText(i.getPageIndex())){System.out.println(reader.readToEnd());}}}
More resources
GitHub examples
You may easily run the code above and see the feature in action in our GitHub examples: