後続のXMLファイルを正しい継承クラスにマップするにはどうすればよいですか。問題は、4つの異なるAPIメソッドがあることです。これは、「api_search_location」という名前のメソッドの1つの応答です。
<?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>
4つの応答タイプすべてに対して次のクラス「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 + "]";
}
}
そして、私はクラス「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 + "]";
}
}
これは私の特定の応答クラスの1つです。
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」を見つけようとしていることを理解しましたが、適切な要素がありません。しかし、XML応答ファイルに応じて、正しい特定のクラス、この場合は「SearchLocationStopsNearbyResponse」を選択する必要があることをSimpleにどのように伝えることができますか?