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?
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: 35258
7
posted
0
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.
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 ]
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
posted
0
I guess this leads to the next question, my good people.
How do I use the Java API to zip/unzip file packages?
from the reference you gave me has solved my most immediate problem. Thanks!
Arthur Buliva
Ranch Hand
Joined: Mar 08, 2006
Posts: 101
posted
0
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: 3011
9
posted
0
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
posted
0
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: 3011
9
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.