如何在 XML 中指定 complexType 列表?

Gab*_*orH 3 java xml xsd jaxb jaxb2

我有 XML 指定以下内容:

    <xs:element name="getNewsResponse">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="newsItem" type="tns:newsList"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:simpleType name="newsList">
        <xs:list itemType="tns:news"/>
    </xs:simpleType>

   <xs:complexType name="news">
        <xs:sequence>
            <xs:element name="id" type="xs:string"/>
            <xs:element name="date" type="xs:string"/>
            <xs:element name="author" type="tns:author"/>
            <xs:element name="title" type="xs:string"/>
            <xs:element name="shortDescription" type="xs:string"/>
            <xs:element name="content" type="xs:string"/>
        </xs:sequence>
    </xs:complexType>
Run Code Online (Sandbox Code Playgroud)

我想在我的回复中列出新闻列表。但是,当我想用​​ jaxb2 创建 Java 对象时,当我运行 mvn clean compile -X 时,xml 返回以下错误:

org.xml.sax.SAXParseException: cos-st-restricts.1.1: The type 'newsList' is atomic, so its {base type definition}, 'tns:news', must be an atomic simple type definition or a built-in primitive datatype.

我应该如何更改我的 XML 以便能够编译?

rod*_*oap 7

除了使用内置列表类型之外,您还可以通过从现有原子类型派生来创建新的列表类型。您不能从现有列表类型或复杂类型创建列表类型。

https://www.w3.org/TR/xmlschema-0/#ListDt

这是来自我的一个工作 XSD,一个拥有多个地址的用户:

<xs:complexType name="user">
    <xs:sequence>
        <xs:element name="addresses" type="tns:addressData" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
Run Code Online (Sandbox Code Playgroud)

请注意,addressData是一个 complexType。

我想这就是你需要的:

<xs:element name="getNewsResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="newsItems" type="tns:news" nillable="true" minOccurs="0" maxOccurs="unbounded"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<xs:complexType name="news">
    <xs:sequence>
        <xs:element name="id" type="xs:string"/>
        <xs:element name="date" type="xs:string"/>
        <xs:element name="author" type="tns:author"/>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="shortDescription" type="xs:string"/>
        <xs:element name="content" type="xs:string"/>
    </xs:sequence>
</xs:complexType>
Run Code Online (Sandbox Code Playgroud)