• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Java And OpenDocument Files

 
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 9626
16
Mac OS X Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OpenOffice has a Java API.
 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how can I use the API to open/edit files through a TextArea on my Java application?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Arthur Buliva
Ranch Hand
Posts: 101
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear Dittmer,

I am simply lost in the array of suggestions in those pages...
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]
     
    Arthur Buliva
    Ranch Hand
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    So whats your suggestion on how to open them?
     
    Ulf Dittmer
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Rancher
    Posts: 43081
    77
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator


    from the reference you gave me has solved my most immediate problem. Thanks!
     
    Arthur Buliva
    Ranch Hand
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Arthur Buliva
    Ranch Hand
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    My redo of the code is




    What could be the issue here as it returns lots of null and empty folders?
     
    Joanne Neal
    Rancher
    Posts: 3742
    16
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 101
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic