JAXB Unmarshalling и elementFormDefault - PullRequest
       7

JAXB Unmarshalling и elementFormDefault

0 голосов
/ 14 ноября 2018

При попытке реализовать сервис SOAP в Spring я столкнулся с интересной проблемой, связанной с XSD и демаршаллингом.Учитывая следующий XSD-файл и плагин jaxb2 maven (версия 2.4)

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:c="http://foo.bar/car"
        targetNamespace="http://foo.bar/car"
        elementFormDefault="qualified">

    <element name="car">
        <complexType>
            <sequence>
                <element name="engine" type="c:engine"/>
            </sequence>
        </complexType>
    </element>

    <complexType name="engine">
        <sequence>
            <element name="type" type="string"/>
            <element name="cylinder" type="positiveInteger"/>
            <element name="attributes" type="c:attributes"/>
        </sequence>
    </complexType>

    <complexType name="attributes">
        <sequence>
            <element name="attribute" minOccurs="0" maxOccurs="unbounded">
                <complexType>
                    <sequence>
                        <element name="name" type="string"/>
                        <element name="value" type="string"/>
                    </sequence>
                </complexType>
            </element>
        </sequence>
    </complexType>

</schema>

Я пытаюсь разобрать после xml

<?xml version="1.0" encoding="UTF-8" ?>
<c:car xmlns:c="http://foo.bar/car">
    <c:engine>
        <type>Big Block</type>
        <cylinder>4</cylinder>
        <attributes>
            <attribute>
                <name>ccu</name>
                <value>469</value>
            </attribute>
            <attribute>
                <name>ps</name>
                <value>500</value>
            </attribute>
        </attributes>
    </c:engine>
</c:car>

На самом деле, я ожидал бы тип полей, цилиндр иатрибуты не должны быть заполнены, так как они определены без пространства имен.Но атрибуты объекта заполняются, и только свойства имени и значения в «атрибуте» остаются нулевыми.

JAXBContext jaxbContext = JAXBContext.newInstance(Car.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Car car = (Car)jaxbUnmarshaller.unmarshal(new File("cars.xml"));

Может ли кто-нибудь объяснить такое поведение, поскольку я не могу найти разумное объяснение этому?

Заранее спасибо.

...