java - XmlRpcClient 解析具有多个变量类型的响应

标签 java xml-parsing xml-rpc xmlrpcclient

我正在使用 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 );

检索结果,但不确定如何迭代每个值以 查找操作状态值。 是否有更优雅的解决方案来使用 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) If you are communicating with other systems

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

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

相关文章:

java - JPQL(Hibernate 实现)选择与连接问题

java - 为什么我不需要 java.base?

java - org.xml.sax.SAXParseException : Premature end of file

java - 使用 DOM 解析器解析 XML 文档,每个标签有多个元素

Java 解析 XML 文档给出 "Content not allowed in prolog."错误

java - java开发桌面应用UI的现有技术(类似html)

java - 如何为 JAR 和调试正确引用资源文件?

c# - 使用 XML-RPC.NET 在参数中传递 CDATA

perl - 我应该使用 CPAN 的替代方案来替换 Frontier::RPC2 吗?

java - 无法在 Android 中登录 OpenERP