1

如何将后续 XML 文件映射到正确的继承类。问题是,我有四种不同的 API 方法,这是名为“api_search_location”的方法之一的响应

<?xml version="1.0" encoding="UTF-8"?>
<ft>
  <response>
    <client device="" appName="" clientId="123" appVersion=""/>
    <responseType>api_search_location</responseType>
    <responseTime>2013-01-21 11:55:43</responseTime>
    <locations status="list">
      <location name="60201040" title="Praterstern" municipality="Wien" type="stop" listIndex="0:1" coordName="wgs84" wgs84Lat="48.21815" wgs84Lon="16.39176" coordX="" coordY="" distanceMeter="" durationMinutes=""/>
      <location name="60201848" title="Praterstern/Lassallestraße" municipality="Wien" type="stop" listIndex="1:2" coordName="wgs84" wgs84Lat="48.21962" wgs84Lon="16.39382" coordX="" coordY="" distanceMeter="" durationMinutes=""/>
    </locations>
    <message messageCode="1">ok</message>
  </response>
</ft>

对于所有四种响应类型,我都有以下类“FtResponse”:

package model.response;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name="ft")
public class FtResponse {
    @Element
    private Response response;

    public FtResponse() {
        super();
    }

    public FtResponse(Response response) {
        super();
        this.response = response;
    }

    public Response getResponse() {
        return response;
    }

    public void setResponse(Response response) {
        this.response = response;
    }

    @Override
    public String toString() {
        return "FtResponse [response=" + response + "]";
    }

}

我有“响应”类。所有特定的响应类都继承自这个通用类:

package model.response;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

    @Root(name = "request")
    public class Response {
        @Element
        private Client client;
        @Element
        private String responseType;
        @Element
        private String responseTime;

        public Response() {
            super();
        }

        public Response(Client client, String responseType, String responseTime) {
            super();
            this.client = client;
            this.responseType = responseType;
            this.responseTime = responseTime;
        }

        public Client getClient() {
            return client;
        }

        public void setClient(Client client) {
            this.client = client;
        }

        public String getResponseType() {
            return responseType;
        }

        public void setResponseType(String responseType) {
            this.responseType = responseType;
        }

        public String getResponseTime() {
            return responseTime;
        }

        public void setResponseTime(String responseTime) {
            this.responseTime = responseTime;
        }

        @Override
        public String toString() {
            return "Response [client=" + client + ", responseType=" + responseType
                    + ", responseTime=" + responseTime + "]";
        }

    }

这是我的特定响应类之一:

package model.response;

import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;

@Root(name = "response")
public class SearchLocationStopsNearbyResponse extends Response {
    @Element
    private Client client;
    @Element
    private String responseType;
    @Element
    private String responseTime;
    @Element
    private LocationCentre locationCentre;
    @Element
    private Locations locations;
    @Element
    private Message message;

    public SearchLocationStopsNearbyResponse() {
        super();
    }

    public SearchLocationStopsNearbyResponse(Client client,
            String responseType, String responseTime,
            LocationCentre locationCentre, Locations locations, Message message) {
        super();
        this.client = client;
        this.responseType = responseType;
        this.responseTime = responseTime;
        this.locationCentre = locationCentre;
        this.locations = locations;
        this.message = message;
    }

    public Client getClient() {
        return client;
    }

    public void setClient(Client client) {
        this.client = client;
    }

    public String getResponseType() {
        return responseType;
    }

    public void setResponseType(String responseType) {
        this.responseType = responseType;
    }

    public String getResponseTime() {
        return responseTime;
    }

    public void setResponseTime(String responseTime) {
        this.responseTime = responseTime;
    }

    public LocationCentre getLocationCentre() {
        return locationCentre;
    }

    public void setLocationCentre(LocationCentre locationCentre) {
        this.locationCentre = locationCentre;
    }

    public Locations getLocations() {
        return locations;
    }

    public void setLocations(Locations locations) {
        this.locations = locations;
    }

    public Message getMessage() {
        return message;
    }

    public void setMessage(Message message) {
        this.message = message;
    }

    @Override
    public String toString() {
        return "SearchLocationStopsNearbyResponse [client=" + client
                + ", responseType=" + responseType + ", responseTime="
                + responseTime + ", locationCentre=" + locationCentre
                + ", locations=" + locations + ", message=" + message + "]";
    }

}

我的问题是,当我尝试将所有数据读入嵌套类时,出现以下异常:

org.simpleframework.xml.core.ElementException: Element 'locationCentre' does not have a match in class model.response.Response at line 7
    at org.simpleframework.xml.core.Composite.readElement(Composite.java:527)
    at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
    at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
    at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
    at org.simpleframework.xml.core.Composite.read(Composite.java:201)
    at org.simpleframework.xml.core.Composite.read(Composite.java:148)
    at org.simpleframework.xml.core.Composite.readVariable(Composite.java:623)
    at org.simpleframework.xml.core.Composite.readInstance(Composite.java:573)
    at org.simpleframework.xml.core.Composite.readUnion(Composite.java:549)
    at org.simpleframework.xml.core.Composite.readElement(Composite.java:532)
    at org.simpleframework.xml.core.Composite.readElements(Composite.java:445)
    at org.simpleframework.xml.core.Composite.access$400(Composite.java:59)
    at org.simpleframework.xml.core.Composite$Builder.read(Composite.java:1383)
    at org.simpleframework.xml.core.Composite.read(Composite.java:201)
    at org.simpleframework.xml.core.Composite.read(Composite.java:148)
    at org.simpleframework.xml.core.Traverser.read(Traverser.java:92)
    at org.simpleframework.xml.core.Persister.read(Persister.java:625)
    at org.simpleframework.xml.core.Persister.read(Persister.java:606)
    at org.simpleframework.xml.core.Persister.read(Persister.java:584)
    at org.simpleframework.xml.core.Persister.read(Persister.java:543)
    at org.simpleframework.xml.core.Persister.read(Persister.java:521)
    at org.simpleframework.xml.core.Persister.read(Persister.java:426)
    at TestRead.main(TestRead.java:19)

我理解 Simple 试图在“Response”类中找到元素“locationCentre”,但没有合适的元素。但是我怎么能对 Simple 说它应该选择正确的特定类,在这种情况下是“SearchLocationStopsNearbyResponse”,具体取决于 XML 响应文件?

4

2 回答 2

1

如果我理解正确,您<responseType>...</responseType>可以决定哪个实现Response是正确的。我猜Simple中没有这样的功能。

工会可能会在这里为您提供帮助。

但是 - 您可以使用Converter或自定义序列化/反序列化的过程Visitor。这可能不像某些注释那么容易,但应该能够解决您的问题。

因此,如果您使用 a ,您必须执行以下操作Converter

  • 实现一个新类,例如。ResponseConverter它实现了Converter-Interface (如果你不需要它,你可以离开write()- 方法)
  • 用注释(例如)标记你的Response-class(以及扩展它的类?),这样简单地知道你想使用你自己的。@Convert@Convert(ResponseConverter)
  • 添加AnnotationStrategy到您的Serializer(例如Serializer ser = new Persister(new AnnotationStrategy());

也可以看看:

于 2013-01-22T16:39:58.383 回答
0

为位置中心尝试@Element(required=false)。它应该工作。

于 2013-01-22T21:14:37.043 回答