I have the following code in one of my classes:
@Text(required=false)
@ElementListUnion({
@ElementList(required = false, inline = true, type = com.company.Child.class, entry="values")
})
public List<Object> valueUnion;
Note that this seems to be the only way to get the framework to work with elements that contain both children and text. This works great when text is present and the elementlist contains elements as well, and produces the following xml:
<parent>
<values>val 1</values>
<values>val 2</values>
some text
</parent>
However, sometimes the element list contains no elements, with only the text being present (meaning the valueUnion List only contains one element, the string of text). This however, results in the following XML:
<parent>
<values />
some text
</parent>
And herein lies the problem, as this causes the server to choke over the empty <values /> tag. Unfortunately I do not have control over the code on the server, and I am looking for a way to force Simple to ignore the empty tag if the elementlist contains no elements.
I have a workaround for you. Its not beautiful, but the concept can help you here.
Instead of element-annotations you can use a custom
Converterwhich serializes your Objects.ExampleClass:(Contains the list, the text and other elements you need)
Actually you only need the
@Convert(ExampleConverter.class)and@RootAnnotations here, since the serialization is done in your own converter (ExampleConverter).ExampleConverterClass:(Serialize / Deserialize your Object here)
(1): This will set your text even if there are some other elements. However this solution may break your formating; the text and the closing tag will be on the same line. You can solve this by inserting a new line.
Create a serializer, et your strategy and write / read
With elements in the list:
Output:
Without elements in the list:
Output:
Please notice the formating “problem” as said before!