如何使用MixedContent数据处理JAXB ComplexType?

我有这个XML结构:

 0.00  17.5% Non-Recoverable vatspecial   

请注意, Description节点具有MixedContent (由文本和XML组成) ,这是关于Description节点的XSD部分

       

此时一切正常, XJC输出生成的类,如下所示,关于TaxDescriptionType

 package org.com.project; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlSchemaType; import javax.xml.bind.annotation.XmlType; import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; /** * 

Java class for TaxDescriptionType complex type. * *

The following schema fragment specifies the expected content contained within this class. * *

 * <complexType name="TaxDescriptionType"> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> * <sequence> * <element name="ShortName" type="{http://www.w3.org/2001/XMLSchema}string"/> * </sequence> * <attribute ref="{http://www.w3.org/XML/1998/namespace}lang"/> * </restriction> * </complexContent> * </complexType> * 

* * */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "TaxDescriptionType", propOrder = { "shortName" }) public class TaxDescriptionType { @XmlElement(name = "ShortName", required = true) protected String shortName; @XmlAttribute(name = "lang", namespace = "http://www.w3.org/XML/1998/namespace") @XmlJavaTypeAdapter(CollapsedStringAdapter.class) @XmlSchemaType(name = "NCName") protected String lang; /** * Gets the value of the shortName property. * * @return * possible object is * {@link String } * */ public String getShortName() { return shortName; } /** * Sets the value of the shortName property. * * @param value * allowed object is * {@link String } * */ public void setShortName(String value) { this.shortName = value; } /** * Gets the value of the lang property. * * @return * possible object is * {@link String } * */ public String getLang() { return lang; } /** * Sets the value of the lang property. * * @param value * allowed object is * {@link String } * */ public void setLang(String value) { this.lang = value; } }

然后,通过上面的class我可以解决这样的元素:

 taxDescriptionType.setLang("en"); taxDescriptionType.setShortName("vatspecial"); /* missing value: 17.5% Non-Recoverable */ 

但问题是我找不到从上面的XML示例中getset MixedContent-ComplexType17.5% Non-Recoverable文本的方法。


这是我尝试过的,它不起作用:

  • 使用mixed="true"属性,如下所示:

(我认为XJC忽略了最后一个属性)


做一些研究,我发现了这个:

JAXB XJC编译器忽略XML Schema文档中的mixed = true

但我不确定这是否是解决这个问题的方法。 其中一个答案说这是一个错误,而另一个答案显示了一个代码,它将MixedContent转换为List ,也许下一个情况将是关于如何处理这个问题:

 taxDescriptionType.getContent().add(Serializable element); 

(我真的不知道如何处理Serializable元素)

如您所述,您需要添加mixed属性以指示您的类型支持混合内容。 如果没有指定,则您的XML内容无效:

       

生成的TaxDescriptionType类将具有以下属性。 实质上,这意味着所有非属性内容都将存储在List 。 这是必要的,因为您需要一种机制来指示文本节点与元素内容的位置。

 @XmlElementRef(name = "ShortName", namespace = "http://www.example.org/schema", type = JAXBElement.class) @XmlMixed protected List content; 

您将使用String (表示文本节点)和JAXBElement (表示元素内容)的实例填充此列表。


另外

混合内容通常会使生活变得更加复杂。 如果可能,我会建议使用备用XML表示。

  0.00  17.5% Non-Recoverable   

要么

  0.00  17.5% Non-Recoverable vatspecial   

使用mixed = true,在ObjectFactory中应该有一个类似JAXBElement createTaxDescriptionTypeShortNameType(ShortNameType)的函数,它会为您生成可序列化元素。

  @XmlElementDecl(namespace = "", name = "shortnametype", scope = TaxDescriptionType.class) public JAXBElement createTaxDescriptionTypeShortNameType(ShortNameType value) { return new JAXBElement(new QName("", "shortnametype"), ShortNameType.class, TaxDescriptionType.class, value); }