Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement for simplifying unbounded sequence #131

Open
o0oGnas opened this issue Oct 26, 2021 · 2 comments
Open

Enhancement for simplifying unbounded sequence #131

o0oGnas opened this issue Oct 26, 2021 · 2 comments

Comments

@o0oGnas
Copy link

o0oGnas commented Oct 26, 2021

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.

@mattrpav
Copy link
Collaborator

JAXB can't 'guess' your intended object model. If you want a sequence of 'paired' items, you should make a complex type and a collection of the complex type.

Try this instead:

<complexType name="Number">
       <attribute name="number" type="int" />
       <attribute name="text" type="string" />
</complexType>

<complexType name="Numbers">
   <sequence>
      <element name="Number" type="ns:Number" maxOccurs="unbounded"/>
   </sequence>
</complexType>

@o0oGnas
Copy link
Author

o0oGnas commented Jan 17, 2022

@mattrpav
I think you didn't fully understand my issue. I'm not asking JAXB to guess my intended object model. I'm asking it to generate unbounded sequences as lists of wrapper objects which wrap the elements inside each sequence.
Creating a complex type to wrap the elements and declaring it as a list with "unbounded" will not solve my problem because the structure would be slightly different from the unbounded sequence. In your suggestion the XML would look something like

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

which is different from what I have as input and thus cannot be unmarshalled by JAXB without doing some hacky workarounds. While I agree this would be the better way to represent the data, it's unfortunately what I have to live with as the structure is defined by a third party and not me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants