2016-02-05 75 views
2

我想deserialize以下xml如何反序列化简单框架中的数组映射?

<scenario name="test responses"> 
    <cmd name="query1"> 
     <return>success_200.xml</return> 
     <return>error_500.xml</return> 
    </cmd> 
    <cmd name="query2"> 
     <return>success_200.xml</return> 
    </cmd> 
</scenario> 

到该类

@Root(name="scenario") 
public class TestScenario { 
    @ElementMap(entry="cmd", key="name", attribute=true, inline=true) 
    private Map<String,StepsList> scenario; 

    @Attribute(required = false) 
    private String name = ""; 

    public static class StepsList { 
     @ElementList(name="return") 
     private List<String> steps = new ArrayList<String>(); 

     public List<String> getSteps() { 
      return steps; 
     } 
    } 
} 

却得到了一个org.simpleframework.xml.core.ValueRequiredException:无法满足@org.simpleframework.xml.ElementList

如何可以做到?

+0

检查:HTTP:// simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#deserialize –

回答

0

于是,几个小时的研究后,我创建了一个有效的解决方案。

奇怪的是,但要创建一个阵列的地图,你需要使用@ElementList装饰与特殊的SimpleFramework工具类Dictionary。插入该字典的对象必须实现接口并可以包含任何解析规则。在我的情况下,它们包含List<String>对应内部<return>标签。

您可以在本教程的阅读工具类:http://simple.sourceforge.net/download/stream/doc/tutorial/tutorial.php#util

@Root(name="scenario") 
public class TestScenario { 
    @ElementList(inline=true) 
    private Dictionary<StepsList> scenario; 

    @Attribute(required = false) 
    private String name = ""; 

    public Dictionary<StepsList> getScenario() { 
     return scenario; 
    } 

    @Root(name="cmd") 
    public static class StepsList implements Entry { 
     @Attribute 
     private String name; 

     @ElementList(inline=true, entry="return") 
     private List<String> steps; 

     @Override 
     public String getName() { 
      return name; 
     } 

     public List<String> getSteps() { 
      return steps; 
     } 
    } 
} 

Dictionary是实现java.util.Set一类,你可以使用它像这样:

TestScenario test = loadScenario("test.xml"); 
String step1 = test.getScenario().get("query1").getSteps().get(0); 
// step1 is now "success_200.xml" 
String step2 = test.getScenario().get("query1").getSteps().get(1); 
// step2 is now "error_500.xml" 
+0

我很感兴趣,你可以吗?明白发生了什么事? –

+0

已更新的答案,以澄清 –

+0

谢谢,但我没有看到地图,但有一个列表> –

0

试试这个:

@ElementList(required = false, inline = true, name="return") 
private List<String> steps = new ArrayList<String>(); 
+0

现在它抛出一个'org.simpleframework.xml.core.ElementException:元素'return'没有匹配' –