SimpleXML Java将类添加为元素

SimpleXML Java将类添加为元素,java,android,xml,simple-framework,Java,Android,Xml,Simple Framework,在我的Android项目中,我有两个XML文件,如下所示: <request> <target>www.facebook.com</target> <packetsize>32</packetsize> <timeout>4</timeout> ... </request> XML模型类: @Root(name = "request") public class PingRespon

在我的
Android
项目中,我有两个
XML
文件,如下所示:

<request>
  <target>www.facebook.com</target>
  <packetsize>32</packetsize>
  <timeout>4</timeout>
  ...
</request>
XML模型类:

@Root(name = "request")
public class PingResponseData {

    @Element
    private PinResponse pinResponse;

    public PinResponse getPinResponse() {
        return pinResponse;
    }
}
但我总是得到一个元素例外:

org.simpleframework.xml.core.ElementException: Element 'target' does not have a match in class

如何将PinResponse类作为元素添加到我的XML模型类中?

通常,这样做的方式如下:

public abstract class PingRequestResponse {

    @Element(name = "target")
    private String target;

    @Element(name = "packetsize")
    private int packetSize;

    @Element(name = "timeout")
    private int timeout;

    ...
}

@Root(name = "request")
public class PingRequest extends PingRequestResponse { }

@Root(name = "response")
public class PingResponse extends PingRequestResponse { }
然而,我还没有充分使用SimpleXML来了解注释是否能在这样的子类中正常工作

试试看会发生什么。

克里斯·拉森帮我解决了我的问题,现在我的最终解决方案是这样的:

基类:(使用子类中的元素的受保护访问)

请求子类:

@Root(name = "request")
public class PingRequest extends PingRequestResponse { 

    public PingRequest(Ping ping) {
            this.target = ping.getTarget();
            this.packetsize = ping.getPacketsize();
            this.timeout = ping.getTimeout();
            ...
}
@Root(name = "response")
public class PingResponse extends PingRequestResponse { 
            //empty as it has the same elements, if it would have additional fields, they would be added here
}
响应子类:

@Root(name = "request")
public class PingRequest extends PingRequestResponse { 

    public PingRequest(Ping ping) {
            this.target = ping.getTarget();
            this.packetsize = ping.getPacketsize();
            this.timeout = ping.getTimeout();
            ...
}
@Root(name = "response")
public class PingResponse extends PingRequestResponse { 
            //empty as it has the same elements, if it would have additional fields, they would be added here
}

谢谢你的回复,我会试试的!
@Root(name = "response")
public class PingResponse extends PingRequestResponse { 
            //empty as it has the same elements, if it would have additional fields, they would be added here
}