分享

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

 印度阿三17 2019-09-29

我有这个XML结构:

<Tax>
  <Money currency="USD">0.00</Money>
  <Description xml:lang="en">
     17.5% Non-Recoverable
    <ShortName>vatspecial</ShortName>
  </Description>
</Tax>

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

<xsd:complexType name="TaxDescriptionType">
  <xsd:sequence>
    <xsd:element name="ShortName" type="xsd:string" />
  </xsd:sequence>
  <xsd:attribute ref="xml:lang" />
</xsd:complexType>

此时一切正常,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;

/**
 * <p>Java class for TaxDescriptionType complex type.
 * 
 * <p>The following schema fragment specifies the expected content contained within this class.
 * 
 * <pre>
 * <complexType name="TaxDescriptionType">
 *   <complexContent>
 *     <restriction base="{http://www./2001/XMLSchema}anyType">
 *       <sequence>
 *         <element name="ShortName" type="{http://www./2001/XMLSchema}string"/>
 *       </sequence>
 *       <attribute ref="{http://www./XML/1998/namespace}lang"/>
 *     </restriction>
 *   </complexContent>
 * </complexType>
 * </pre>
 * 
 * 
 */
@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./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;
    }

}

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

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

但问题是我找不到从上面的XML示例中获取或设置MixedContent-ComplexType的17.5%不可恢复文本的方法.

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

>使用mixed =“true”属性,如下所示:

< xsd:complexType name =“TaxDescriptionType”mixed =“true”>

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

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

JAXB XJC compiler disregarding mixed=true on XML Schema documents

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

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

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

解决方法:

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

<xsd:complexType name="TaxDescriptionType" mixed="true">
    <xsd:sequence>
        <xsd:element name="ShortName" type="xsd:string" />
    </xsd:sequence>
    <xsd:attribute ref="xml:lang" />
</xsd:complexType>

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

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

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

另外

混合内容通常会使生活变得更加复杂.如果可能,我会推荐一个替代XML表示.

<Tax>
  <Money currency="USD">0.00</Money>
  <Description xml:lang="en" ShortName="vatspecial">
    17.5% Non-Recoverable
  </Description>
</Tax>

要么

<Tax>
  <Money currency="USD">0.00</Money>
  <Description xml:lang="en">
    <LongName>17.5% Non-Recoverable</LongName>
    <ShortName>vatspecial</ShortName>
  </Description>
</Tax>
来源:https://www./content-4-474251.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多