Skip to content

Enhancement for simplifying unbounded sequence #131

@o0oGnas

Description

@o0oGnas

Suppose we have a complex type in a schema like this.

<complexType name="Numbers">
    <sequence maxOccurs="unbounded">
       <element name="number" type="int" />
       <element name="text" type="string" />
    </sequence>
</complexType>

By default XJC will generate a List<JAXBElement> inside class Numbers to represent the sequence. When used with simplify plugin, it becomes 2 separate lists List<Integer> number and List<String> text. While this works for unmarshalling XML data, it doesn't keep the interconnected relationship between each number and text element as defined by the schema, which can make it awkward to program with. Furthermore, when the same object is marshalled back into XML, all number elements will be placed before all text elements, which is wrong.

For example, XML data such as

<numbers>
    <number>1</number>
    <text>one</number>
    <number>2</number>
    <text>two</text>
</numbers>

would be marshalled back as

<numbers>
    <number>1</number>
    <number>2</number>
    <text>one</number>
    <text>two</text>
</numbers>

My proposed enhancement for this use case is as follows:

  • Generate a static inner class called Sequence inside generated Numbers class
  • This class would contain 2 fields: number as Integer and text as String.
  • The Numbers class would instead contain a List<Numbers.Sequence>.
  • e.g.
public class Numbers {
    List<Numbers.Sequence> sequence;

    public static class Sequence {
        Integer number;
        String text;
    }   
}

One problem I can think of with this approach is when the complexType happens to have an attribute also named sequence. In such cases I think prefixing or suffixing the name of the field List<Numbers.Sequence> sequence with something would suffice.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions