JAXB解组了无序元素,其中一个元素出现多次

时间:2015-07-08 12:04:19

标签: java xml jaxb unmarshalling

我有一个xml:

<package>
  <metadata>
    <title>Title</title>
    <language>en</language>
    <meta name="cover" />
  </metadata>
</package>

正确xml的另一个例子:

    <package>
      <metadata>
        <language>en</language>
        <title>Title</title>
      </metadata>
    </package>

    <package>
      <metadata>
        <meta name="content" />
        <language>en</language>
        <meta name="cover" />
        <title>Title</title>
      </metadata>
    </package>

我想使用JAXB库将其解组为java类。 困难在于&#34; meta&#34;元素可以是0到多个元素的顺序&#34; title&#34; &#34;语言&#34;和&#34; meta&#34; s也可以是随机的。

我的班级看起来像:

@XmlRootElement(name = "package")
@XmlAccessorType(XmlAccessType.FIELD)
public class Test {
    @XmlElement(name = "metadata", required = true)
    public Metadata metadata;

    @XmlAccessorType(XmlAccessType.FIELD)
    @XmlType(propOrder={})
    public static class Metadata {
        @XmlElement(name = "title", required = true)
        public String title;

        @XmlElement(name = "language", required = true)
        public String language;

        @XmlElement(name = "meta", required = false)
        public List<Meta> metas;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Meta {
       @XmlAttribute(name = "name", required = true)
       public String name;
    }
}

它与架构匹配:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="package" type="test"/>

  <xs:complexType name="test">
    <xs:sequence>
      <xs:element name="metadata" type="metadata"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="metadata">
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="language" type="xs:string"/>
      <xs:element name="meta" type="meta" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="meta">
    <xs:sequence/>
    <xs:attribute name="name" type="xs:string" use="required"/>
  </xs:complexType>
</xs:schema>

当我解组文档时,我收到错误:

org.xml.sax.SAXParseException: cos-all-limited.2: The {max occurs} of an element in an 'all' model group must be 0 or 1. The value 'unbounded' for element 'meta' is invalid.

如果删除属性@XmlType(propOrder = {}),则它与架构匹配:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="package" type="test"/>

  <xs:complexType name="test">
    <xs:sequence>
      <xs:element name="metadata" type="metadata"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="metadata">
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="language" type="xs:string"/>
      <xs:element name="meta" type="meta" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="meta">
    <xs:sequence/>
    <xs:attribute name="name" type="xs:string" use="required"/>
  </xs:complexType>
</xs:schema>

如果属性的顺序不同,我会收到错误:

Invalid content was found starting with element 'language'. One of '{title}' is expected.

我无法更改xml文件,但我可以修改类和/或注释。我动态地通过这个类生成模式,因此模式也可以调整。

任何想法如何实现理想的行为?

0 个答案:

没有答案