aspose file tools*
The moose likes Java in General and the fly likes Java And OpenDocument Files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Java And OpenDocument Files" Watch "Java And OpenDocument Files" New topic
Author

Java And OpenDocument Files

Arthur Buliva
Ranch Hand

Joined: Mar 08, 2006
Posts: 101
Hi Friends,

Now, lets say you have a file, lets call it document.odt

I have noticed that .odt files are not "stand alone" files like .txt or .dat files. Rather they are like zipped files with other defining files in there.

Before I get out of track, let me go to my question.

document.odt contains some text, say "Hello World". I want to edit this text through Java and save the resulting text. How can this be achieved?

Thanks and regards.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8927
    
    9

OpenOffice has a Java API.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
Arthur Buliva
Ranch Hand

Joined: Mar 08, 2006
Posts: 101
And how can I use the API to open/edit files through a TextArea on my Java application?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42289
    
  64
Accessing ODF files through the OO Java API and using the data in GUI elements are separate activities. You'll need to dig into the API to figure out how to access the data that you need for the GUI. The AccessingFileFormats wiki page links to a number of articles about the OO Java API and the ODF file format.


Ping & DNS - my free Android networking tools app
Arthur Buliva
Ranch Hand

Joined: Mar 08, 2006
Posts: 101
Dear Dittmer,

I am simply lost in the array of suggestions in those pages...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14273
    
  21

The OpenOffice Java API
  • OpenOffice can read a number of file formats, and makes them accessible through its API. A starting point might be this article and of course the OO developer site
  • Some introductory information about the OO file format can be found here and here - basic Java code for reading OO files is here
  • Reading an OpenOffice file is not as simple as reading a plain text file, simply because OpenOffice contains a lot more features than a plain text editor.
    [ October 31, 2007: Message edited by: Jesper Young ]

    Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
    Arthur Buliva
    Ranch Hand

    Joined: Mar 08, 2006
    Posts: 101
    So whats your suggestion on how to open them?
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42289
        
      64
    Either use the OO Java API to convert the file to some other format that you do know how to open, or -based on the articles linked above- write code that opens and processes the files.

    From my cursory look at the articles OO files appear to be zipped-up XML files. Since Java has APIs for dealing with ZIP and XML files, getting at the actual contents shouldn't be too hard. Making sense of those is a different matter, of course - I'd recommend to start with a simple document, to see if you can manage to extract whatever information you need.
    Arthur Buliva
    Ranch Hand

    Joined: Mar 08, 2006
    Posts: 101
    I guess this leads to the next question, my good people.

    How do I use the Java API to zip/unzip file packages?
    Ulf Dittmer
    Marshal

    Joined: Mar 22, 2005
    Posts: 42289
        
      64
    Examples of using the ZIP API -as well as just about all other java.* classes- can be found at the Developer's Almanac: http://www.exampledepot.com/egs/java.util.zip/pkg.html
    Arthur Buliva
    Ranch Hand

    Joined: Mar 08, 2006
    Posts: 101


    from the reference you gave me has solved my most immediate problem. Thanks!
    Arthur Buliva
    Ranch Hand

    Joined: Mar 08, 2006
    Posts: 101
    This is a sample output:



    and the code



    retrieves only the first entry whereas I would want the file called content.xml to be extracted, if not the entire odt file, onto a specified folder. How or what do I need to modify in the code?
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3682
        
      16
    You need a loop. ZipInputStream.getNextEntry() returns null when there are no more entries, so you can use that as the loop controller.
    If you only want to output certain files, then you need to check the name of each ZipEntry object before you write it out. Check the API docs to see if there is a method that gets the name of the ZipEntry object.


    Joanne
    Arthur Buliva
    Ranch Hand

    Joined: Mar 08, 2006
    Posts: 101
    My redo of the code is




    What could be the issue here as it returns lots of null and empty folders?
    Joanne Neal
    Rancher

    Joined: Aug 05, 2005
    Posts: 3682
        
      16
    You create a new FileOutputStream object every time through the loop, but you only close the last one after you exit the loop. Put the out.close() call inside the loop.
    Arthur Buliva
    Ranch Hand

    Joined: Mar 08, 2006
    Posts: 101
    There is a piece of software called JODConverter that can open any file that OpenOffice can open and export to any that OpenOffice can export to. I am happy to say that it has really helped me solve the issues.


    PS: It is FOSS
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Java And OpenDocument Files