hemu
hemu

Reputation: 3261

Java : Simple XML Serialization(simple-xml-2.6.6.jar) gives error with XML file containing <SOAP-ENV:Body> & <SOAP-ENV:Envelope>

I am using Simple XML Serialization(simple-xml-2.6.6.jar) here to convert my XML response from webservice to POJO class. XML is :

 <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 contains list of appointments. POJOs are as follow :

@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;
    }   
}

And Appointment is :

@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;
    }


}

Now If I remove <SOAP-ENV:Envelope> & <SOAP-ENV:Body> from my XML it works fine. But when parsing XML with these tag I get the error saying :

Exception in thread "main" org.simpleframework.xml.core.ElementException: Element 'Body' does not have a match in class pojo.Return at line 3

Upvotes: 2

Views: 1913

Answers (1)

Robert Massaioli
Robert Massaioli

Reputation: 13487

Now If I remove <SOAP-ENV:Envelope> & <SOAP-ENV:Body> from my XML it works fine. But when parsing XML with these tag I get the error.

And that is your problem right there. Simple XML attempts to parse the entire document that you give it. If you say:

serializer.read(Appointment.class, yourXML)

and the root of that XML is <SOAP-ENV:Envelope> followed by a <SOAP-ENV:Body> then it is not going to work. You need to make one or more classes to encapsulate the <SOAP-ENV:*> nodes in you XML. Otherwise Simple XML is going to get confused and tell you that it is not seeing what it expected to see. That is why it works when you remove those elements: because the XML now looks exactly like it expected it to be.

Upvotes: 2

Related Questions