2

我在这里使用简单 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
4

1 回答 1

2

现在,如果我从我的 XML中删除<SOAP-ENV:Envelope>&它工作正常。<SOAP-ENV:Body>但是当使用这些标签解析 XML 时,我得到了错误。

这就是你的问题。简单 XML 尝试解析您提供给它的整个文档。如果你说:

serializer.read(Appointment.class, yourXML)

并且该 XML 的根<SOAP-ENV:Envelope>之后是 a<SOAP-ENV:Body>然后它不起作用。您需要创建一个或多个类来封装<SOAP-ENV:*>XML 中的节点。否则,简单 XML 会感到困惑,并告诉您它没有看到预期的内容。这就是当您删除这些元素时它会起作用的原因:因为 XML 现在看起来与预期的完全一样。

于 2012-11-06T23:08:33.597 回答