JAXB Parsing - Strange Behavior

I get valid XML from the server. let's say

Xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
    <xs:element name="rootElement">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="elementOne"/>
                <xs:element ref="elementTwo"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="elementOne" type="elementOneType"/>
    <xs:complexType name="elementOneType">
        <xs:sequence>
            <xs:element ref="childOne" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:element name="childOne" type="childOneType"/>
    <xs:complexType name="childOneType" mixed="true">
        <xs:attribute name="One" type="xs:string" use="optional"/>
        <xs:attribute name="Two" type="xs:NMTOKEN" use="optional"/>
        <xs:attribute name="Three" type="xs:string" use="optional"/>
    </xs:complexType>
    <xs:element name="elementTwo" type="elementTwoType"/>
    <xs:complexType name="elementTwoType">
        <xs:sequence>
            <xs:element name="childTwo" type="childTwoType" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
    <xs:complexType name="childTwoType" mixed="true">
        <xs:sequence>
            <xs:element ref="nestedChild" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="One" type="xs:string" use="optional"/>
        <xs:attribute name="Two" type="xs:NMTOKEN" use="optional"/>
        <xs:attribute name="Three" type="xs:string" use="optional"/>
    </xs:complexType>
    <xs:element name="nestedChild" type="nestedChildType"/>
    <xs:complexType name="nestedChildType">
        <xs:attribute name="test" type="xs:string" use="required"/>
    </xs:complexType>
</xs:schema>

XML

<?xml version="1.0" encoding="UTF-8"?>
<rootElement>
    <elementOne>
        <childOne One="532" Two="938" Three="changed"/>
    </elementOne>
    <elementTwo>
        <childTwo One="532" Two="532" Three="68d53"/>
    </elementTwo>
</rootElement>

Can someone guide me, what could be the reason? I can't debug where to start. nestedChild is optional. so whenever JAXB parses xml, it parses it in such a way that if my xml does not have a nested Child, it shows 1 (empty object). if it has one nested shape, it shows 2, one real and the other empty. the conversion of the actual child is OK, and obviously, when the system tries to convert the empty object, it throws an exception.

its jaxb v 1.3

+3
source share

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


All Articles