javaxml-parsingxml-rpcxmlrpcclient

XmlRpcClient parse response with multiple Variable types


I'm using XmlRpcClient to connect to a XML-RPC server In the response to my request in the XML response I will get:

int cpuLoad, 
String fanStatus, 
String fanStatusWorst, 
String temperatureStatus, 
String temperatureStatusWorst,
String rtcBatteryStatus,
String rtcBatteryStatusWorst, 
String voltagesStatus, 
String voltagesStatusWorst, 
String operationalStatus

I'm using

HashMap result = (HashMap) xmlRpcClient.execute(METHOD_DEVICE_HEALTH, params );

to retrieve results, but not sure how can I iterate over each value to find operationalStatus value. Is there a more elegant solution to parse results using xmlRpcClient? So I just retrieve the fields I'm interested?

Thanks


Solution

  • Actually there are more than one answer to your question depending on your cenario.

    1) If you are communicating with Java based system using XML-RPC
    

    Apache XML-RPC library comes with handy class called ClientFactory. This class allows you to bind your service interface (in your case the interface of the object you are invoking METHOD_DEVICE_HEALTH) to an object in the JVM of your consumer (the object that invokes xmlRpcClient.execute(METHOD_DEVICE_HEALTH, params)). For more details about the implementation, take a look at this link.

    In this cenario, you can even have an transfert object like

    public class DeviceHealth {
      int cpuLoad, 
      String fanStatus, 
      String fanStatusWorst, 
      String temperatureStatus, 
      String temperatureStatusWorst,
      String rtcBatteryStatus,
      String rtcBatteryStatusWorst, 
      String voltagesStatus, 
      String voltagesStatusWorst, 
      String operationalStatus
    
      // getters and setters
    }
    

    and a method in your service class returning directly its type

    public interface HealthService {
       DeviceHealth checkDeviceHealth(int id);
    }
    
    public class DefaultHealthService implements HealthService {
        public DeviceHealth checkDeviceHealth(int id) {}
    }
    

    The framework will serialize and deserialize this object automatically for you.

    2) If you are communicating with other systems
    

    Well, in this case your gona have to handle the serialization and deserialization process your own. You can use for that JAXB, which has a reference implementation built-in JDK 1.6 and 1.7. This post gives you an idea on how to do this.