我正在尝试正确注释我的类,这样我就可以在android框架中使用SimpleXML。我的问题是,我总是:
E/something(25192): Could not read [class at.something.scanner.Issues]; nested exception is org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=issues, required=true, type=void) on field 'issues' private java.util.List at.something.scanner.Issues.issues for class at.something.scanner.Issues at line 1和
E/something(25192): Caused by: org.simpleframework.xml.core.ValueRequiredException: Unable to satisfy @org.simpleframework.xml.ElementList(data=false, empty=true, entry=, inline=true, name=issues, required=true, type=void) on field 'issues' private java.util.List at.something.scanner.Issues.issues for class at.something.scanner.Issues at line 1我知道我可以通过使用@ElementList(required=false)来摆脱required=false,但是它不能解决我的问题,因为我最终得到了一个空列表。
请求使用spring:
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(new StringHttpMessageConverter());
Serializer serializer = new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
restTemplate.getMessageConverters().add(new SimpleXmlHttpMessageConverter(serializer));
ResponseEntity<Issues> response = restTemplate.exchange(url, HttpMethod.GET, new HttpEntity<Object>(requestHeaders), Issues.class);
Issues tickets = response.getBody();Issues.java:
@Root
public class Issues{
@ElementList(name="issues", inline=true)
private List<Issue> issues;
public void setIssues(List<Issue> results) {
this.issues = results;
}
public List<Issue> getIssues() {
return issues;
}
}Issue.java
@Root(name="issue")
public class Issue{
@Attribute(name="id")
private String id;
}来自服务器的XML响应示例:
<issues>
<issue id="ABC-10">
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="SingleField" name="Assignee">
<value>someone</value>
</field>
</issue>
<issue id="ABC-11">
<field xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="SingleField" name="reporterName">
<value>someone</value>
</field>
</issue>
</issues>我希望有人能给我一个如何解决我的问题的提示。
发布于 2014-04-08 08:39:35
使用
@ElementList(name="issues", inline=true, entry="Issue")我也遇到了同样的问题,这对我来说很有好处。在Stackoverflow网站上找到了这个答案。
编辑:
我已经用简单的XML尝试了您的示例,对于类问题,您已经使用了
@Element(name="field")
private Field field;失踪了。
public class Field {
@Element(name = "value", required = true)
private String value;
@Attribute(name = "name", required = false)
private String name;
}我还用了
public static void main(String args[]) throws Exception {
Serializer serializer = new Persister(new Format("<?xml version=\"1.0\" encoding= \"UTF-8\" ?>"));
File source = new File("sample.xml");
Issues i = serializer.read(Issues.class, source);
}我没有任何错误。使用来自xml的数据正确地加载了对象。
https://stackoverflow.com/questions/22924024
复制相似问题