2

I'm trying to use the simpleframework to serialize a third party (parasoft) xml report.

<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?

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?

2
  • There is no testSuites tag in your xml? Commented Apr 28, 2015 at 19:08
  • 2
    the problem is that TestSuite can exist as an Element or an ElementList Commented Apr 28, 2015 at 19:18

1 Answer 1

3

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)
  2. Nested TestSuite elements

I have reduced the problem a bit for this answer. Let's assume a Xml like this:

<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)


The code to this:

TestEntry (just an Interface)

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

@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.

Finally, you can add the outer TestSuite and - of course - complete the attributes.

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).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.