0

I'm using cxf xjc plugin version 4.0 to transform a schema to a java class, but this is generating a class that when a list is null, it returns an empty list, but I need the null, so I'm using JAXB binding to try to avoid this and get null when the list is empty or null,

the object containing MyList in my schema looks like:

<xs:complexType name="SomeObject">
  <xs:sequence>
    <xs:element maxOccurs="7" minOccurs="0" name="MyList" type="SomeType"/>
    ...

And SomeType is declared as:

<xs:simpleType name="SomeType">
  <xs:restriction base="xs:string">
    <xs:minLength  value="1"/>
    <xs:maxLength  value="50"/>
    ....

JAXB is generating a List<String> with this schema for MyList, but it's automatically returning empty list instead of null, I need to figure out how to avoid this. I cannot change the schema, also cannot change generated classes.

I'm trying to use an adapter for this, using bindings:

<jxb:bindings version="3.0"
              xmlns:jxb="http://jakarta.ee/xml/ns/jaxb"
              xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <jxb:bindings schemaLocation="my-schema.xsd" 
                  node="/xs:schema/xs:complexType[@name='SomeObject']/xs:sequence/xs:element[@name='MyList']">
        <jxb:property>
            <jxb:baseType>
                <jxb:javaType name="java.util.List"
                              parseMethod="com.example.NullIfEmptyListAdapter.unmarshal"
                              printMethod="com.example.NullIfEmptyListAdapter.marshal"/>
            </jxb:baseType>
        </jxb:property>
    </jxb:bindings>
</jxb:bindings>

but this makes myList to be a List<List>.

And if I change javaType name to java.lang.String, then MyList is a <List<String>, but the adapter doesn't work, because NullIfEmptyListAdapter is for <List<String>, List<String>> and Adapter1 autogenerated is for <String, String>.

So, using it as I have it right now, almost works, except it generates myList as a List<List>>.

I need it null because I'm marshalling the class to an xml string, which is what I actually need, and empties create an empty tag which is invalid for schema.

The schema is way too big to use wrappers, that's why I would like to tackle MyList directly, but I don't have that much experience on XML to Java transformations and JABX bindings and stuff, I have tried in many many ways, this is the closest to work.

Like if I can just have MyList returning null instead of empty list, would be great.

Any suggestions much apprecciated.

4
  • Why can't you change the schema from : minOccurs="0" to: minOccurs="1" Commented Feb 2 at 11:31
  • Because it is not a required field @jdweng Commented Feb 2 at 16:00
  • Every item in the schema is verified. Attributes are optional so you do not get an error if it doesn't exist, but is validated if it does exist : w3schools.com/xml/schema_simple_attributes.asp Commented Feb 3 at 0:17
  • So, you are saying if SomeObject is present, but it doesn't have a MyList in it, even with minOccurs="1", it won't fail when validated? What would be the point of minOccurs="1" then? @jdweng Commented Feb 4 at 14:31

0

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.