simple ODT manipulation

499 views
Skip to first unread message

James Conner

unread,
Jan 17, 2011, 6:23:12 PM1/17/11
to jopend...@googlegroups.com

I’ve been looking through the code and samples and haven’t found what I’m looking for.

Maybe someone can help, or give me an example.

I need to open an ODT file and

1. Replace key words with new values (global search/replace).

2. If certain key word(s) present in a TABLE, replace them with new values, and possibly insert new rows into the table.

 

I have found:

getDescendantByName("table:table", "Table1")

but that won’t allow me to scan all tables in the document.

 

Once I locate the tables, it looks like I can use

duplicateRows(start, count, copies)

 

followed by:

table.getCellAt(0, 1).setValue("xyz");

 

to update the values.  Is that correct?

 

Then saveAs (File f) to write out the result.

 

Thanks!

 

Daedac

unread,
Jan 20, 2011, 2:19:35 PM1/20/11
to jOpenDocument
I have found the solution:

I will send you an e-mail with the necessary files to work the test.

I dont write the solution because it is a little large. In addition a
odt file is necessary.

If somebody want the files, I can send you it.

Cheers.

Arun

unread,
Feb 10, 2011, 11:06:36 AM2/10/11
to jOpenDocument

Please send the files to me also.

Expecting reply at the earliest,

James Conner

unread,
Feb 11, 2011, 11:29:17 AM2/11/11
to jopend...@googlegroups.com
I was able to do what I needed using jopendocument and xpath. Here is a
quick summary.

0. ODSingleXMLDocument xmldoc = new ODPackage(new
File("input.odt")).toSingle();

1. use xpath.selectNodes to find the elements: tables are:
".//table:table"
and other elements are: ".//text:p|.//text:span|.//text:h"
e.g.
xmldoc.getXPath(".//text:p").selectNodes(xmldoc.getDocument().getRootEle
ment());

2. for each table t returned by the xpath query:
Int rowCount = t.getRowCount();
Int colCount = t.getColumnCount();
MutableCell<ODSingleXMLDocument> cell = t.getCellAt (col, row);
Cell.replaceBy(old, new);

Also
t.duplicateRows(row, 1, duplicateCount) as necessary if you need to
expand the table.

3. for each element e returned by the xpath query:
e.setText(e.getText().replace (old, new))

4. xmldoc.saveAs(File f)

My imports look like this:
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.xpath.XPath;
import org.jopendocument.dom.ODPackage;
import org.jopendocument.dom.ODSingleXMLDocument;
import org.jopendocument.dom.spreadsheet.MutableCell;
import org.jopendocument.dom.spreadsheet.Table;
import org.jopendocument.model.OpenDocument;

Reply all
Reply to author
Forward
0 new messages