Java trick for reading OpenOffice.org files

I have read a few times about people having problems reading the ZIPed XML files in OpenOffice.org documents. The problem is SAX parsers not being able to locate a local copy of the office.dtd file. I have been using a kluge to get around this problem for a long time and have not had any problems with it: When reading the input stream from the ZIP file entry labeled "content.xml", skip past the second ">" character:

InputSource is =
new InputSource(zf.getInputStream(zipEntry));
InputStream r = is.getByteStream();
for (int i=0, count = 0; i<500; i++) {
if ((char)r.read() == '>') count++;
if (count > 1) break;
}
SAXParser p = saxFactory.newSAXParser();
p.parse(r, new OpenOffice.OpenOfficeSaxHandler());
Hopefully in the future people having this problem will find this post when doing a web search and save themselves a little time. Another good alternative is to make office.dtd available on your system and put it on your classpath.

Comments

  1. Hi Mark,

    i'm working on lucene project for searching in open office documents.I have followed your instruction from an article on http://www.devx.com/java/Article/27728/1954, but in that i didn't get what is MyDTD file.

    Problem is that my application is searching fine in pdf,doc,rtf,xml and txt. But its not searching in .odt, .sxw,.ppt etc. basically all open office documents.

    please help

    Regards,

    Dipti

    ReplyDelete

Post a Comment

Popular posts from this blog

My Dad's work with Robert Oppenheimer and Edward Teller

Time and Attention Fragmentation in Our Digital Lives

I am moving back to the Google platform, less excited by what Apple is offering