首页 > 解决方案 > JAXB - XJC - 将 XML 加密模式映射到 Java 类

问题描述

我正在尝试使用 XJC将用于 XML 加密的 XML 模式映射到要与 JAXB ( https://www.w3.org/TR/xmlenc-core/xenc-schema.xsd ) 一起使用的 Java 类中。我对EncryptionMethodType类型的映射有点困惑。在模式中,它被定义为

  <complexType name='EncryptionMethodType' mixed='true'>
    <sequence>
      <element name='KeySize' minOccurs='0' type='xenc:KeySizeType'/>
      <element name='OAEPparams' minOccurs='0' type='base64Binary'/>
      <any namespace='##other' minOccurs='0' maxOccurs='unbounded'/>
    </sequence>
    <attribute name='Algorithm' type='anyURI' use='required'/>
  </complexType>

XJC 生成以下类型(为简洁起见,已删除注释):

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "EncryptionMethodType", propOrder = {
    "content"
})
public class EncryptionMethodType {

    @XmlElementRefs({
        @XmlElementRef(name = "KeySize", namespace = "http://www.w3.org/2001/04/xmlenc#", type = JAXBElement.class, required = false),
        @XmlElementRef(name = "OAEPparams", namespace = "http://www.w3.org/2001/04/xmlenc#", type = JAXBElement.class, required = false)
    })
    @XmlMixed
    @XmlAnyElement(lax = true)
    protected List<Object> content;

    @XmlAttribute(name = "Algorithm", required = true)
    @XmlSchemaType(name = "anyURI")
    protected String algorithm;

    /**
     * Objects of the following type(s) are allowed in the list
     * {@link JAXBElement }{@code <}{@link BigInteger }{@code >}
     * {@link JAXBElement }{@code <}{@link byte[]}{@code >}
     * {@link String }
     * {@link Object }
     */
    public List<Object> getContent() {
        if (content == null) {
            content = new ArrayList<Object>();
        }
        return this.content;
    }

    public String getAlgorithm() {
        return algorithm;
    }

    public void setAlgorithm(String value) {
        this.algorithm = value;
    }
}

我不是 JAXB 专家(而且文档很少而且很糟糕),但由于KeySizeOAEPparams元素的基数为 0 或 1,我希望它们成为 Java 代码中的单独属性,而将List<Object>用于无界##other元素。

是否有任何参数或自定义绑定我可以应用来获得更简单的 Java 表示,即像这样的?

class EncryptionMethodType {
   public BigInteger KeySize;
   public byte[] OAEPparams;
   public List<Object> otherContent;
}

提前致谢!

标签: javaxsdjaxbxjc

解决方案


推荐阅读