LAB-5569: ODFDOM - Changing ODF documents using the new opensourced multi-tiered API

Expected Duration: 100 minutes

Exercise 3: Build a Web Report from Spreadsheet Data (20 minutes)

 

The goal of this exercise is to create a web page from the data available in a spreadsheet document. For this, the ODFDOM API will be used.


Steps to Follow

 
  1. Create a new Netbeans Web Application Project named 'Report' like in exercise 2.

    Figure-01: Create a web based project.

  2. Add the odfdom.jar and xercesImpl.jar libraries to the project like in exercise 2.

    • <lab_root>/resources/odfdom/jars/xercesImpl.jar
    • <lab_root>/resources/odfdom/jars/odfdom.jar

    For JavaOne attendees, the libraries are here:

    • /export/home/lab5569/odfdom/resources/odfdom/jars/xercesImpl.jar
    • /export/home/lab5569/odfdom/resources/odfdom/jars/odfdom.jar

    Click "OK" afterwards.


    Figure-05: Add required libraries to the project

  3. Add the imports to the 'index.jsp' file like in exercise 2.

     <%@page import="java.text.*,
                     java.util.*,
                     org.w3c.dom.*,
                     org.odftoolkit.odfdom.dom.OdfNamespace,
                     org.odftoolkit.odfdom.doc.OdfDocument,
                     org.odftoolkit.odfdom.doc.OdfFileDom,
                     org.odftoolkit.odfdom.doc.element.text.*,
                     org.odftoolkit.odfdom.doc.element.office.*,
                     org.odftoolkit.odfdom.doc.element.table.*,
                     javax.xml.xpath.*" %>
                            
  4. Change the text of the h1 element to 'Address Data Report' and insert an empty table behind it.

  5. After the closing <h1> tag, enter the java code to open the report.ods spreadsheet document in the <lab_root>/resources folder. Get the content file dom from the document. Note: Make sure to enclose the java code using '<%' and '%>'.

                 ////////////////////////////////////////////////////////////////
                 // Start ODFDOM handling
                 // 1 Load source document
                 OdfDocument document = OdfDocument.loadDocument("/export/home/lab5569/odfdom/resources/report_data.ods");
                 // 2 Get the Content DOM of this source document
                 OdfFileDom content = document.getContentDom();
    
                            
  6. Use a xpath expression to receive a NodeList of all table rows:

                 // 3 Find the data rows
                 XPath xpath = XPathFactory.newInstance().newXPath();
                 xpath.setNamespaceContext(new OdfNamespace());
                 NodeList rows = (NodeList)xpath.evaluate("//table:table[1]//table:table-row", content, XPathConstants.NODESET);
    
                            
  7. Iterator over the table rows and cells. If a new row starts, use 'out.print("<tr>")' to start a new table row. If a row ends, use 'out.print("</tr>")' to close the respective table row tag. If a new cell starts, use 'out.print("<th>")' or 'out.print("<td>")'to start a new table (header) cell. If a cell ends, close the respective cell tag. Inside the cell you have to print the cell contents:

                 // 4 Print data to report table
                 out.print("<table border=\"1\">");
                 for (int i = 0; i < rows.getLength(); ++i){
                    OdfTableRow row = (OdfTableRow)rows.item(i);
                    out.print("<tr>");
                    NodeList tableRowChilds = row.getChildNodes();
                    for (int j = 0; j < tableRowChilds.getLength(); ++j){
                        OdfTableCell cell = (OdfTableCell)tableRowChilds.item(j);
                        out.print(i == 0 ? "<th>" : "<td>");
                        String textContent = cell.getTextContent();
                        out.print(textContent);
                        out.print(i == 0 ? "</th>" : "</td>");
                    }
                    out.print("</tr>");
                 }
                 out.print("</table>");
    
                            
  8. You can now run the web application by pressing F6. If the application does not what you expect it to do, you can debug the code by pressing STRG+F5. The output should look like this:

    Figure-03: The created report


Summary

 

In this exercise, you learned how to use the ODFDOM API to load, manipulate and store documents.

 

Back to top
Next exercise