JAXB throws an error when unmarshalling empty attributes int, double or date

I ran into a problem when JAXB was undoing XML data.

JAXB throws an exception when unmarshalling a null value for an attribute int, doubleor datefrom xml. For example, it throws java.lang.NumberFormatExceptionwhen it discards the following XML data.

<sku displayName="iphone" price=""/>

Below is my diagram:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="sku" type="SkuType" maxOccurs="unbounded"/>
    <xs:complexType name="SkuType">
        <xs:attribute name="displayName" type="xs:string" use="required"/>
        <xs:attribute name="price" type="xs:double" use="required"/>
        <xs:attribute name="startDate" type="xs:dateTime" use="optional"/>
        <xs:attribute name="minimumOrderQty" type="xs:integer" use="optional"/>
    </xs:complexType>
</xs:schema>

Sorry for the messy xml. I can’t type the “left corner” sign at the entrance. Can anyone help me out?

Many thanks.

+3
source share
2 answers

- , "" . , .

price = " , price =" 0", .

:

<sku displayName="iphone" price="0"/>

:

<xs:attribute name="price" type="xs:double" use="optional"/>
+2

. - XML-, , . :

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:element name="product" type="product"/>

   <xsd:complexType name="product">
      <xsd:attribute name="name" type="xsd:string"/>
      <xsd:attribute name="price" type="emptyInt"/>
   </xsd:complexType>

   <xsd:simpleType name="emptyInt">
      <xsd:union>
         <xsd:simpleType>
            <xsd:restriction base="xsd:integer"/>
         </xsd:simpleType>
         <xsd:simpleType>
            <xsd:restriction base="xsd:token">
               <xsd:enumeration value=""/>
            </xsd:restriction>
         </xsd:simpleType>
      </xsd:union>
   </xsd:simpleType>
</xsd:schema>
+1

Source: https://habr.com/ru/post/1754717/


All Articles