【问题标题】:With JAXB how do we avoid JAXBElement for mixed complexType使用 JAXB,我们如何避免混合 complexType 的 JAXBElement
【发布时间】:2020-08-30 16:36:56
【问题描述】:

当在 XSD 中将 complexType 标记为 mixed="true" 时,生成的 Java 类会生成一个 List,其中包含“String”类型或围绕实际值的 JAXBElement 包装器。如果没有 mixed="true",则不会生成 JAXBElement,并且值会直接映射到类型类。例如:

    <xs:complexType name="div" mixed="true">
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
            <xs:element name="a" minOccurs="0" maxOccurs="unbounded" type="anchorType"/>
     </xs:sequence>
     ...
   </complexType>

这里的 div 类型将包含一个 List getContent() 方法,该方法返回一个字符串或 AnchorType 的 JAXBElement。

我希望它只返回没有 JAXBElement 的 String 或 AnchorType。

【问题讨论】:

  • 为什么不直接从 'div' 的类型定义中删除 'mixed=true' 呢?
  • 我需要能够放入纯文本和元素 - 类似于 HTML div 元素

标签: java xsd jaxb jaxb2-maven-plugin jaxb2-basics


【解决方案1】:

考虑这个包含 div 标签和一些子标签的 XML:

<parentTag>
  <div>myPlainTextValue<a/><a/></div>
</parentTag>

但 XML 文档中的混合内容不一定出现在单个块中。 根据 XSD,此 XML 也是有效的:

<parentTag>
  <div>my<a/>Plain<a/>Text<a/>Value</div>
</parentTag>

在这两种情况下,标签“div”的文本内容都是“myPlainTextValue”。但在第二个示例中,它分布在子标签之间的 4 个较小的块中。 JAXB 需要生成可以处理 general 情况的代码,所以我不希望它为您的情况提供特殊模式。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2011-05-18
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多