1

I have a Class with list of Integers. This class is created from XSD below.

  <xs:complexType name="Vehicle">
    <xs:sequence>
      <xs:element name="regNum" type="xs:string" />
      <xs:element name="company" type="xs:string" />
      <xs:element name="model" type="xs:string" />
      <xs:element name="lights" type="vehicle:Lights" />
    </xs:sequence>
  </xs:complexType>

  <xs:simpleType name="Lights">
    <xs:list>
      <xs:simpleType>
        <xs:restriction base="xs:int">
          <xs:minInclusive value="1" />
        </xs:restriction>
      </xs:simpleType>
    </xs:list>
  </xs:simpleType>

This generated following class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Vehicle", propOrder = {
    "regNum",
    "company",
    "model",
    "lights"
})
@XmlSeeAlso({
    com.vcc.siig.domain.manual.vehicle.Car.class,
    com.vcc.siig.domain.manual.vehicle.Truck.class
})
public class Vehicle {

    @XmlElement(required = true)
    protected String regNum;
    @XmlElement(required = true)
    protected String company;
    @XmlElement(required = true)
    protected String model;
    @XmlList
    @XmlElement(type = Integer.class)
    @XmlSchemaType(name = "anySimpleType")
    protected List<Integer> lights;

    ...
}

When I marshal from Vehicle Object to XML. I get following error:

Exception in thread "main" java.lang.ClassCastException: java.lang.Integer incompatible with java.lang.String
    at com.sun.xml.internal.bind.v2.model.impl.RuntimeBuiltinLeafInfoImpl$StringImplImpl.print(RuntimeBuiltinLeafInfoImpl.java:982)
    at com.sun.xml.internal.bind.v2.runtime.reflect.ListTransducedAccessorImpl.print(ListTransducedAccessorImpl.java:115)
    at com.sun.xml.internal.bind.v2.runtime.reflect.DefaultTransducedAccessor.writeLeafElement(DefaultTransducedAccessor.java:66)
    at com.sun.xml.internal.bind.v2.runtime.property.ListElementProperty.serializeBody(ListElementProperty.java:111)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:337)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsXsiType(XMLSerializer.java:680)
    at com.sun.xml.internal.bind.v2.runtime.property.SingleElementNodeProperty.serializeBody(SingleElementNodeProperty.java:147)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeBody(ClassBeanInfoImpl.java:337)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsSoleContent(XMLSerializer.java:575)
    at com.sun.xml.internal.bind.v2.runtime.ClassBeanInfoImpl.serializeRoot(ClassBeanInfoImpl.java:318)
    at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:476)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:326)
    at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:255)
    at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:107)
    at com.kishor.temp.MarkX.main(MarkX.java:44)

If make Vehicle.lights as null, I get XML.

I'm not sure if this is bug or issue with my xsd.

Kishor Prakash
  • 8,011
  • 12
  • 61
  • 92
  • the thing is that you get back a list of `Strings` and not `Integers` and from there you get that error. Check this answer that may help you: https://stackoverflow.com/questions/7182533/using-jaxb-generated-class-for-an-element-that-requires-an-integer-with-a-pattern/7194004 – Edwin May 07 '18 at 14:49
  • 1
    Try removing `@XmlSchemaType(name = "anySimpleType")`. – lexicore May 07 '18 at 16:47
  • 1
    The `anySimpleType` is an issue. You may not be able to modify generated code each time, so also see [this answer](https://stackoverflow.com/a/30832608/1110636) that suggests an alternate way to define lists of simple types. – Timir May 08 '18 at 20:20
  • @lexicore : Can I remove it `@XmlSchemaType` using `XJC` or shall I modify it? – Kishor Prakash May 23 '18 at 15:48
  • @KishorPrakash I'm not quite sure what would work. You can modify the annotation using [tag:jaxb2-annotate-plugin]. Removing the annotation is not supported yet (here's the [issue](https://github.com/highsource/jaxb2-annotate-plugin/issues/26) for this). – lexicore May 23 '18 at 16:14

0 Answers0