2

ここでは Simple XML Serialization(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>

Appointments には、予定のリストが含まれています。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 のルートの後に a が<SOAP-ENV:Envelope>続く<SOAP-ENV:Body>場合、それは機能しません。<SOAP-ENV:*>XML 内のノードをカプセル化するには、1 つ以上のクラスを作成する必要があります。そうしないと、Simple XML が混乱し、期待どおりに表示されていないことがわかります。これが、これらの要素を削除しても機能する理由です。XML が期待どおりに見えるようになったからです。

于 2012-11-06T23:08:33.597 に答える