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!
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;