Thursday, July 7, 2011

Generate a XML file for a view object

There is a need of generating a xml file from a view object. This is a very easy process as there is a method of view object class “writeXml” to write the content of view object to a xml format.

  1. First generate Impl class for application module.
  2. Write following code to create an object of view object, and generate xml content. Then write this generated xml to a file using output stream.
public String printXml() {

        Connection conn;
        FileOutputStream out;
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            AppModuleImpl am = (AppModuleImpl)resolvEl("AppModuleDataControl");
            conn = am.getDBTransaction().createStatement(0).getConnection();
            ViewObject vo = am.findViewObject("EmployeeView");
            ((XMLNode)vo.writeXML(4, XMLInterface.XML_OPT_ALL_ROWS)).print(outputStream);
            System.out.println(outputStream);
            out = new FileOutputStream("F://Employee.xml");
            PrintStream p = new PrintStream(out);
            p.println(outputStream);
            p.close();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return null;
    }

  public Object resolvEl(String data){
    FacesContext fc = FacesContext.getCurrentInstance();
    Application app = fc.getApplication();
    ExpressionFactory elFactory = app.getExpressionFactory();
    ELContext elContext = fc.getELContext();
    ValueExpression valueExp = elFactory.createValueExpression(elContext, "#{data."+data+".dataProvider}", Object.class);
   return valueExp.getValue(elContext);
  }

No comments:

Post a Comment