2 Replies Latest reply on Oct 31, 2006 1:31 PM by ntsiii

    displaying dom objects generated from java in flex tree control

    stationcharlie
      I have written a POJO that returns hiearchial data from a database in the form of a org.w3c.dom object. I have deployed this object on my server and configured the remoting service. The remote object is being invoked sucessfully. However I am not able to populate the tree control with the dom object. The dom object is generated according to the default data description as provided in adobe manuals. Please help in advising how to populate the tree control. The code is reproduced below.
      ------------------------------------------------------------------------------------------ ---------------
      POJO Code for generating dom xml object
      ------------------------------------------------------------------------------------------ ---------------
      package accounts.xmlconvert;

      import java.sql.*;

      import javax.xml.parsers.DocumentBuilder;
      import javax.xml.parsers.DocumentBuilderFactory;
      import javax.xml.parsers.ParserConfigurationException;

      import org.w3c.dom.Document;
      import org.w3c.dom.Element;
      import org.w3c.dom.Text;


      import com.sun.org.apache.xml.internal.serialize.OutputFormat;
      import com.sun.org.apache.xml.internal.serialize.XMLSerializer;


      public class XMLConvert {

      public Document AccountService() throws Exception {

      // String i="Null";
      Document dom;
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();
      dom = db.newDocument();

      Element rootEle = dom.createElement("root");
      dom.appendChild(rootEle);

      //System.out.println(rootEle);


      //------------------------------------------------------------------------------
      Class.forName("org.postgresql.Driver");
      String url = "jdbc:postgresql:postgres";

      Connection con = DriverManager.getConnection(url,"system","manager");

      Statement stmt = con.createStatement();
      ResultSet res = stmt.executeQuery("select * from accountlist order by code");



      while(res.next()) {

      String code = res.getString(1);
      String description = res.getString(2);
      int parentcode = res.getInt(3);
      String type = res.getString(4);


      if(parentcode == 0) {

      Element AccountListEle = dom.createElement("Groups");
      AccountListEle.setAttribute("label",description);
      AccountListEle.setAttribute("Id", code);
      AccountListEle.setIdAttribute("Id",true);
      rootEle.appendChild(AccountListEle);

      }

      if(parentcode > 0) {
      if(type.equals("g")) {
      //do something
      PreparedStatement state = con.prepareStatement("select code,description from accountlist where code = ?");
      state.setInt(1,parentcode);

      ResultSet result = state.executeQuery();

      while(result.next()) {

      Element foundParent = dom.getElementById(result.getString(1));
      Element newChild = dom.createElement("Groups");
      newChild.setAttribute("label",description);
      newChild.setAttribute("Id",code);
      newChild.setIdAttribute("Id",true);
      foundParent.appendChild(newChild);
      }
      result.close();
      state.close();

      }
      if(type.equals("a")) {
      //do something
      PreparedStatement state = con.prepareStatement("select code,description from accountlist where code = ?");
      state.setInt(1,parentcode);

      ResultSet result = state.executeQuery();

      while(result.next()) {

      Element foundParent = dom.getElementById(result.getString(1));
      Element newChild = dom.createElement("Account");
      newChild.setAttribute("label",description);
      newChild.setAttribute("Id",code);
      newChild.setIdAttribute("Id",true);
      foundParent.appendChild(newChild);
      }
      result.close();
      state.close();


      }


      }


      }

      //------------------------------------------------------------------------------
      /* //print
      OutputFormat format = new OutputFormat(dom);
      format.setIndenting(true);

      //to generate output to console use this serializer
      XMLSerializer serializer = new XMLSerializer(System.out, format);
      serializer.serialize(dom); */


      res.close();

      stmt.close();

      con.close();

      return dom;

      }
      }


      ---------------------------------------------------------------------------------------
      the mxml application code
      ---------------------------------------------------------------------------------------

      <?xml version="1.0"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml">

      <mx:RemoteObject id="xmlconv" destination="convertxml"/>

      <mx:XMLListCollection id="capitalColl" source="{xmlconv.AccountService.lastResult}"/>

      <!-- The XMLListCollection does not include the XML root. -->
      <mx:Tree id="Tree2" dataProvider="{capitalColl}" labelField="@label"
      width="300"/>
      <mx:Button click="xmlconv.AccountService()"/>

      </mx:Application>

      ------------------------------------------------------------------------------------------ -------------
      Destination config
      ------------------------------------------------------------------------------------------ -------------
      <destination id="convertxml">
      <properties>
      <source>accounts.xmlconvert.XMLConvert</source>

      </properties>
      </destination>

      ------------------------------------------------------------------------------------------ ------------
      i get this error when i click the button
      ------------------------------------------------------------------------------------------ ------------

      [RPC Fault faultString="java.lang.NullPointerException : null" faultCode="Server.Processing" faultDetail="null"]
      at mx.rpc::AbstractInvoker/ http://www.adobe.com/2006/flex/mx/internal::faultHandler()
      at mx.rpc::Responder/fault()
      at mx.rpc::AsyncRequest/fault()
      at ::NetConnectionMessageResponder/NetConnectionChannel.as$37:NetConnectionMessageResponder: :statusHandler()
      at mx.messaging::MessageResponder/status()

      ------------------------------------------------------------------------------------------ ------------------------------------------
      if you are interested you may create a table in your database and execute the following sql statements (this has been created in
      postgresql 8.1)
      ------------------------------------------------------------------------------------------ -------------------------------------------

      create table accountlist ( code integer, description varchar(64), parentcode integer, type character(1));

      insert into table values (1,'Assets',0,'g');
      insert into table values (2,'Liabilities',0,'g');
      insert into table values (3,'Income',0,'g');
      insert into table values (4,'Expenses',0,'g');
      insert into table values (5,'Cash Balances',1,'g');
      insert into table values (6,'Main Cash',5,'g');
      insert into table values (7,'Share Capital',2,'g');
      insert into table values (8,'BPO Services',3,'g');
      insert into table values (9,'Software Development',3,'g');
      insert into table values (10,'Other Income',3,'g');
      insert into table values (11,'Sale of Old Newspapers',10,'g');
      insert into table values (12,'Sale of Scrap',10,'g');

      ------------------------------------------------------------------------------------------ ------------------------------
      Please help with populating the tree control in flex.

      Thanks in advance.