我正在使用XmlRpcClient连接到XML-RPC服务器
在XML响应中对我的请求的响应中,我将得到:

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


我在用着

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


检索结果,但不确定如何将每个值迭代到
查找operationalStatus值。
有没有更优雅的解决方案来使用xmlRpcClient解析结果?所以我只检索我感兴趣的字段?

谢谢

最佳答案

实际上,根据您的情况,您的问题有多个答案。

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
}


和服务类中的方法直接返回其类型

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

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


该框架将自动为您序列化和反序列化此对象。

2)如果您正在与其他系统通信


好吧,在这种情况下,您必须自己处理序列化和反序列化过程。您可以使用该JAXB,它具有内置JDK 1.6和1.7的参考实现。 This post让您了解如何执行此操作。

关于java - XmlRpcClient解析具有多个变量类型的响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13064050/

10-09 00:57