user1576720
user1576720

Reputation: 115

How do you parse the contents of a .odt file into a string in Java?

Preferable using the ODFDOM API. I would like to have the entire file's contents in a string, if possible. If not, how would you search the file for a specific substring?

Thanks in advance.

Upvotes: 2

Views: 3637

Answers (2)

Moussa
Moussa

Reputation: 4154

The accepted answer give me an empty String. Here is how I managed to get odf file text content

import java.io.*;
import org.odftoolkit.odfdom.doc.OdfDocument;
import org.odftoolkit.odfdom.incubator.doc.text.OdfEditableTextExtractor;

public static String getOdfDocumentText(File file)
{
    try (OdfDocument document = OdfDocument.loadDocument(file))
    {
        OdfEditableTextExtractor extractor = OdfEditableTextExtractor.newOdfEditableTextExtractor(document);
        return extractor.getText();
    }
    catch (Exception exception)
    {
        System.err.println(exception.getMessage());
    }
    return "";
}

Upvotes: 0

Jasonw
Jasonw

Reputation: 5064

you will need to load the odt document and then get the content root. From there, get the text content which will return you a string. So that should give you an idea on how to search using string? For example:

TextDocument document = TextDocument.loadDocument("test.odt");
String texts = document.getContentRoot().getTextContent());

Upvotes: 0

Related Questions