Java : Simple XML Serialization(simple-xml-2. 6.6.jar) 包含 <SOAP-ENV:Body> 和 <SOAP-ENV:Envelope> 的 XML 文件出现错误

标签 java android web-services xml-parsing

我正在使用简单 XML 序列化(simple-xml-2.6.6.jar) here将我的 XML 响应从 webservice 转换为 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;
    }


}

现在如果我删除 <SOAP-ENV:Envelope> & <SOAP-ENV:Body>从我的 XML 来看它工作得很好。但是当使用这些标签解析 XML 时,我收到错误消息:

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

最佳答案

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.

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

serializer.read(Appointment.class, yourXML)

该 XML 的根是 <SOAP-ENV:Envelope>随后是 <SOAP-ENV:Body>那么它就行不通了。您需要创建一个或多个类来封装 <SOAP-ENV:*> XML 中的节点。否则 Simple XML 将会变得困惑并告诉您它没有看到它期望看到的内容。这就是为什么当您删除这些元素时它会起作用:因为 XML 现在看起来与预期的完全一样。

关于Java : Simple XML Serialization(simple-xml-2. 6.6.jar) 包含 <SOAP-ENV:Body> 和 <SOAP-ENV:Envelope> 的 XML 文件出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12947320/

相关文章:

java - checkstyle 警告 "Declaring variables, return values or parameters of type ' ArrayList' is not allowed."是什么意思?

java - 用 Java 构建棋盘

web-services - 是否有用于检索 S&P 500 成分股代码的免费 API?

java - File.list() 为目录返回 null

java - zipalign 未随 Java SDK/JRE 一起安装

android - jfeinstein的SlidingMenu库如何自定义setSecondaryMenu的宽度

java - 在 ConstraintLayout 上添加 facebook 受众网络横幅时出错

android - 如何创建没有主键的实体的新实例?

c# - ASMX Web 服务的自定义 WSDL

sql-server - SSRS 无法有条件地从 XML 数据源中过滤 "nil"值?