struct in array - usage?

  • Anonymous - 2011-07-27


    I want to fetch some data from a xmlrpc service, and I can successfully get the data when the return is a struct.
      int "id"
      string "name"

    The code I use is:

    public HashMap<Object, Serializable> getDetails(Integer serverId) {
        try {
            HashMap<Object, Serializable> result = (HashMap<Object, Serializable>) this.rpc.invoke("system.getDetails", 
                                                new Object[] { this.sessionKey, serverId });
            return result;
        } catch (XmlRpcException e) {
            return null;
    API api = new API();
    XmlRpcStruct details = (XmlRpcStruct) api.getDetails(100000);
    Set<?> set = details.entrySet();
    Iterator<?> it = set.iterator();
    while(it. hasNext()) {
      Map.Entry<?, ?> me = (Map.Entry<?, ?>);
      System.out.println(me.getKey() + " : " + me.getValue());

    But I have no idea on how to get the results when the return is a struct whithin an array:
        int "id"
        string "name"
    I know that a XmlRpcArray is an ArrayList but I dont know how to use it in this case.
    Can someone please explain?


  • Greger Olsson

    Greger Olsson - 2011-07-27

    Hi Claes,

    Since XmlRpcArray is a simple ArrayList you can use the get(int index) method to fetch a value from it, and then typecast it into whatever type you expect at that index from the service.

    XmlRpcArray, however, have helper methods to do this typecasting for you (which is essentially the only service XmlRpcArray has over the original ArrayList), so if your service returns an XML-RPC Array with an XML-RPC Struct in the first element of the result, the following code would work:

    // However you invoke your service (XmlRpcClient, XmlRpcProxy or your own wrapper)
    XmlRpcArray arrayFromService = ...;
    // Fetch the Struct from the first element:
    XmlRpcStruct structFromArray = arrayFromService.getStruct(0);
    // Use the struct as a HashMap or with the helper methods:
    int someValue = structFromArray.getInteger("someKeyInStruct");

    Hope that helps,

  • Anonymous - 2011-07-28

    Hi Greger!

    Thanks for the quick reply! :)
    I changed my code to use the helper methods instead and it is much cleaner now..

    public XmlRpcStruct getDetails2(int id) throws XmlRpcException, XmlRpcFault {
        XmlRpcStruct result;
        result = (XmlRpcStruct) this.rpc.invoke("system.getDetails", new Object[] {this.sessionKey, id});
        return result;
    XmlRpcStruct det = (XmlRpcStruct) api.getDetails2(id);
    String details = det.getString("description");

    I also tried your code and it works very well, thanks!


  • Anonymous - 2011-07-28

    Last comment should read;
    I tried your code on struct in array and it works very well..

    public XmlRpcArray searchByName(String name) throws XmlRpcException, XmlRpcFault {
        XmlRpcArray result = (XmlRpcArray) this.rpc.invoke("system.searchByName", new Object[] { this.sessionKey, name });
        return result;
    XmlRpcArray search = (XmlRpcArray) api.searchByName("whatever");
    XmlRpcStruct searchStruct = search.getStruct(0);
    String name = searchStruct.getString("name");

    Thanks again!



Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks