我在这里使用简单 XML 序列化(simple-xml-2.6.6.jar)将我的 XML 响应从 web 服务转换为 POJO 类。XML是:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/1999/XMLSchema" xmlns:xsi="http://www.w3.org/1999/XMLSchema-instance">
<SOAP-ENV:Body>
<return>
<appointments>
<appointment>
<encounterId>211707</encounterId>
<date>2012-10-16</date>
<startTime>00:00:00</startTime>
<ufname>Sam</ufname>
<ulname>Willis</ulname>
<reason>Chest Congestion</reason>
<FacilityId>2837</FacilityId>
<Notes/>
</appointment>
</appointments>
</return>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
约会包含约会列表。POJO如下:
@Root
public class Return {
@ElementList
private List<Appointment> appointments;
@ElementList
private List<Appointment> historicalAppointments;
public List<Appointment> getAppointments() {
return appointments;
}
public void setAppointments(List<Appointment> appointments) {
this.appointments = appointments;
}
public List<Appointment> getHistoricalAppointments() {
return historicalAppointments;
}
public void setHistoricalAppointments(List<Appointment> historicalAppointments) {
this.historicalAppointments = historicalAppointments;
}
}
约会是:
@Root
public class Appointment {
@Element(required=false)
int encounterId;
@Element
Date date;
@Element
String startTime;
@Element(required=false)
String endTime;
@Element
String ufname;
@Element
String ulname;
@Element(required=false)
int FacilityId;
@Element(required=false)
String reason;
@Element(required=false)
String Notes;
@Element(required=false)
String Status;
@Element(required=false)
int EncLock;
public int getEncounterId() {
return encounterId;
}
public void setEncounterId(int encounterId) {
this.encounterId = encounterId;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public String getEndTime() {
return endTime;
}
public void setEndTime(String endTime) {
this.endTime = endTime;
}
public String getUfname() {
return ufname;
}
public void setUfname(String ufname) {
this.ufname = ufname;
}
public String getUlname() {
return ulname;
}
public void setUlname(String ulname) {
this.ulname = ulname;
}
public int getFacilityId() {
return FacilityId;
}
public void setFacilityId(int facilityId) {
FacilityId = facilityId;
}
public String getReason() {
return reason;
}
public void setReason(String reason) {
this.reason = reason;
}
public String getNotes() {
return Notes;
}
public void setNotes(String notes) {
Notes = notes;
}
public String getStatus() {
return Status;
}
public void setStatus(String status) {
Status = status;
}
public int getEncLock() {
return EncLock;
}
public void setEncLock(int encLock) {
EncLock = encLock;
}
}
现在,如果我从我的 XML中删除<SOAP-ENV:Envelope>
&它工作正常。<SOAP-ENV:Body>
但是当使用这些标签解析 XML 时,我收到错误消息:
Exception in thread "main" org.simpleframework.xml.core.ElementException: Element 'Body' does not have a match in class pojo.Return at line 3