简体   繁体   English

SimpleFramework XML嵌套对象

[英]SimpleFramework XML nested objects

I'm trying to use the simpleframework to serialize a third party (parasoft) xml report. 我正在尝试使用simpleframework来序列化第三方(parasoft)xml报告。

<TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" pass="42" total="42">
  <TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" name="Test Suite: APIs">
    <TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0">
      <Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" />
      <Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" id="wk:///2" name="Test 2: GetControlParams" pass="1" tool="GetControlParams" total="1" />
    </TestSuite>
    <TestSuite authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0">
      <Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" name="Test 1: GetHouseInfo" pass="1" tool="GetHouseInfo" total="1" />
      <Test authChange="" authFail="" change="0" changePass="0" changeTotal="0" fail="0" name="Test 2: GetHouseInfo" pass="1" tool="GetHouseInfo" total="1" />
    </TestSuite>
  </TestSuite>
</TestSuite>

How do I handle the recursive relationship for TestSuite? 如何处理TestSuite的递归关系?

I've tried 我试过了

@Element(name = "TestSuite", required = false)
private TestSuite testSuite;

@ElementList(inline = true, entry = "Test", required = false)
private List<Test> test;

@ElementList(inline = true, entry = "TestSuite", required = false)
private List<TestSuite> testSuites;

But am running up against the error 但是我遇到了错误

Exception in thread "main" org.simpleframework.xml.core.PersistenceException: Duplicate annotation of name 'TestSuite' on field 'testSuites' private java.util.List TestSuite.testSuites

Any thoughts? 有什么想法吗?

I've tried [...] But am running up against the error 我已经尝试过[...]但正在遇到错误

This is intended: You have two annotations with the same (tag-)name but different types. 这旨在:您有两个具有相同(标签)名称但类型不同的注释。 Which one should the serializer choose? 串行器应该选择哪一个?


There are two issues to address: 有两个问题要解决:

  1. Elements have some required and some optional arguments (Solution: use required argument of simples annotations) 元素具有一些必需参数和一些可选参数(解决方案:使用简单注释的required参数)
  2. Nested TestSuite elements 嵌套的TestSuite元素

I have reduced the problem a bit for this answer. 我为这个答案减少了一些问题。 Let's assume a Xml like this: 让我们假设这样的Xml:

<TestSuite change="1" name="suite lvl 2">
    <TestSuite change="0">
        <Test name="test1" />
        <Test name="test2" fail="0" />
    </TestSuite>
</TestSuite>

The trick to this: use @ElementListUnion and some kind of Interface (or abstract base class) 诀窍是:使用@ElementListUnion 某种接口 (或抽象基类)


The code to this: 代码如下:

TestEntry (just an Interface) TestEntry(只是一个接口)

public interface TestEntry
{
}

TestCase 测试用例

@Root(name = "Test")
public class TestCase implements TestEntry
{
    @Attribute(required = false)
    private int fail;
    @Attribute
    private String name;

    // Getter etc.
}

TestSuite 的TestSuite

@Root(name = "TestSuite")
public class TestSuite implements TestEntry
{
    @Attribute
    private int change;
    @Attribute(required = false)
    private String name;
    @ElementListUnion({
        @ElementList(inline = true, required = false, type = TestCase.class, name = "TestCase"),
        @ElementList(inline = true, required = false, type = TestSuite.class, name = "TestSuite")
    })
    private List<TestEntry> content;

    // Getter etc.
}

You see the trick? 你明白了吗? Now you can have either a TestCase or TestSuite in that list. 现在,您可以在该列表中使用TestCaseTestSuite

Finally, you can add the outer TestSuite and - of course - complete the attributes. 最后,您可以添加外部TestSuite并且-当然-完成属性。

Btw. 顺便说一句。 if you run into trouble deciding whether an elements is of Type X or Y - a Converter is still an option (but more to write manually). 如果您在确定某个元素是X类型还是Y类型时遇到麻烦-仍然可以选择Converter (但可以手动编写)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM