ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-09-29 00:12:55  阅读:285  来源: 互联网

标签:xjc java xml jaxb xsd


我有这个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>
 * &lt;complexType name="TaxDescriptionType">
 *   &lt;complexContent>
 *     &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
 *       &lt;sequence>
 *         &lt;element name="ShortName" type="{http://www.w3.org/2001/XMLSchema}string"/>
 *       &lt;/sequence>
 *       &lt;attribute ref="{http://www.w3.org/XML/1998/namespace}lang"/>
 *     &lt;/restriction>
 *   &lt;/complexContent>
 * &lt;/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.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;
    }

}

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

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.example.org/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>

标签:xjc,java,xml,jaxb,xsd
来源: https://codeday.me/bug/20190928/1829774.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有