Export table as XML file in ADF

Since I wrote about how to export table to Excel in Oracle ADF, it has been one of the most read blog posts. I know that the export of data is an important feature in an application, this way, I will show how to export rows (all or some) of the table as an XML file.

Download the sample application: ADFXMLApp.zip.

Create a new ADF application that will use data of employees.

export-table-to-xml-in-adf1

Go to EmployeesView View Object and generate the View Object Implementation Class.

export-table-to-xml-in-adf2

Copy the following code inside EmployeesViewImpl class.

public RowSet filterById(String id) {
  ViewCriteria myCriteria = this.createViewCriteria();
  myCriteria.setName("myCriteria");
  ViewCriteriaRow criteriaRow = myCriteria.createViewCriteriaRow();
  criteriaRow.setAttribute("EmployeeId", "IN (" + id + ")");
  myCriteria.addElement(criteriaRow);

  ViewRowSetImpl rs = (ViewRowSetImpl) findByViewCriteria(myCriteria, -1, ViewObject.QUERY_MODE_SCAN_VIEW_ROWS);

  removeRowSet(rs);
  rs.setName("EmployeesRowSet");
  addRowSet(rs);

  return rs;
}

export-table-to-xml-in-adf3

The empView.jsf file contains an employees table and two buttons to export rows as XML file.

export-table-to-xml-in-adf4

Go to source tab and remove the selectedRowKeys property of af:table tag.
If you don’t remove it, when you try to get all selected rows from table, the getSelectedRowKeys method will return only the last row selected.

export-table-to-xml-in-adf5

The EmployeesBean class contains two methods.

public void exportSelectedRowsToXML(FacesContext facesContext, OutputStream outputStream) {
  RichTable table = (RichTable) JSFUtils.findComponentInRoot("t1");
  RowKeySet rksSelectedRows = table.getSelectedRowKeys();
  Iterator itrSelectedRows = rksSelectedRows.iterator();

  StringBuilder id = new StringBuilder("");
  while (itrSelectedRows.hasNext()) {
    Key key = (Key) ((List) itrSelectedRows.next()).get(0);
    id.append(key.getKeyValues()[0]);

    if (itrSelectedRows.hasNext()) {
      id.append(",");
    }
  }

  DCIteratorBinding dcIteratorBindings = ADFUtils.findIterator("EmployeesView1Iterator");
  ViewObject vo = dcIteratorBindings.getViewObject();
  RowSet rs = ((EmployeesViewImpl) vo).filterById(id.toString());

  try {
    ((XMLNode) (rs.writeXML(0, XMLInterface.XML_OPT_ALL_ROWS))).print(outputStream);
  } catch (IOException e) {
    e.printStackTrace();
  }
}

public void exportAllRowsToXML(FacesContext facesContext, OutputStream outputStream) {
  DCIteratorBinding dcIteratorBindings = ADFUtils.findIterator("EmployeesView1Iterator");
  ViewObject vo = dcIteratorBindings.getViewObject();

  try {
    ((XMLNode) (vo.writeXML(0, XMLInterface.XML_OPT_ALL_ROWS))).print(outputStream);
  } catch (IOException e) {
    e.printStackTrace();
  }
}

export-table-to-xml-in-adf6

Now, when you run the application, you can export all rows as an XML File.

export-table-to-xml-in-adf7

You can export only the selected rows as an XML File too.

export-table-to-xml-in-adf8

To change the default XML element names, use the custom properties: XML_ELEMENT and XML_ROW_ELEMENT.

export-table-to-xml-in-adf9

export-table-to-xml-in-adf10

export-table-to-xml-in-adf11

Waslley Souza

Author: Waslley Souza

Consultor Oracle com foco em tecnologias Oracle Fusion Middleware e SOA. Certificado Oracle WebCenter Portal, Oracle ADF e Java.

Leave a Reply

Your email address will not be published. Required fields are marked *