请参阅下面的代码和CPUUsage
课程。当我在main
方法中运行此代码时,所有字段都会返回零,而不是“我想要的”1,2,3,4,5“。我需要更改字段类型吗?或者可能添加一些其他类型的注释?或其他什么?
String testXml =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
"<env:Envelope xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\">" +
"<env:Body>" +
"<dp:response xmlns:dp=\"http://www.datapower.com/schemas/management\">" +
"<dp:timestamp>2015-09-09T04:33:48-04:00</dp:timestamp>" +
"<dp:status>" +
"<CPUUsage xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
"<tenSeconds>1</tenSeconds>\n" +
"<oneMinute>2</oneMinute>\n" +
"<tenMinutes>3</tenMinutes>\n" +
"<oneHour>4</oneHour>\n" +
"<oneDay>5</oneDay>" +
"</CPUUsage>" +
"</dp:status>" +
"</dp:response>" +
"</env:Body>" +
"</env:Envelope>";
CPUUsage cpuUsage = JAXB.unmarshal(new StringReader(testXml), CPUUsage.class);
System.out.println(cpuUsage);
CPUUsage类:
@XmlRootElement(name = "CPUUsage")
class CPUUsage {
private int tenSeconds;
private int oneMinute;
private int tenMinutes;
private int oneHour;
private int oneDay;
@XmlElement(name = "tenSeconds")
public int getTenSeconds() {
return tenSeconds;
}
public void setTenSeconds(int tenSeconds) {
this.tenSeconds = tenSeconds;
}
@XmlElement(name = "oneMinute")
public int getOneMinute() {
return oneMinute;
}
public void setOneMinute(int oneMinute) {
this.oneMinute = oneMinute;
}
@XmlElement(name = "tenMinutes")
public int getTenMinutes() {
return tenMinutes;
}
public void setTenMinutes(int tenMinutes) {
this.tenMinutes = tenMinutes;
}
@XmlElement(name = "oneHour")
public int getOneHour() {
return oneHour;
}
public void setOneHour(int oneHour) {
this.oneHour = oneHour;
}
@XmlElement(name = "oneDay")
public int getOneDay() {
return oneDay;
}
public void setOneDay(int oneDay) {
this.oneDay = oneDay;
}
@Override
public String toString() {
return "tenSeconds:" + tenSeconds + "|oneMinute:" + oneMinute + "|tenMinutes:" + tenMinutes + "|oneHour:" + oneHour + "|oneDay:" + oneDay;
}
}
答案 0 :(得分:1)
想出来......如果我只是删除所有信封信息并从<CPUUsage>
元素开始,代码就可以了。因此,将testXml
修改为如下所示可以解决我的问题:
String testXml =
"<CPUUsage xmlns:env=\"http://www.w3.org/2003/05/soap-envelope\">\n" +
"<tenSeconds>1</tenSeconds>\n" +
"<oneMinute>2</oneMinute>\n" +
"<tenMinutes>3</tenMinutes>\n" +
"<oneHour>4</oneHour>\n" +
"<oneDay>5</oneDay>" +
"</CPUUsage>";