如何使用MixedContent数据处理JAXB ComplexType? [英] How to deal with JAXB ComplexType with MixedContent data?

查看:106
本文介绍了如何使用MixedContent数据处理JAXB ComplexType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种XML结构:

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

请注意描述节点已包含 MixedContent (由文字和XML组成),这是关于<$的 XSD 部分c $ c>说明节点

Notice that Description node has MixedContent (composed with text and XML) and this is the XSD part regarding Description node:

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

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

Everything is ok at this point, XJC outputs the generated classes like this one regarding 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;
    }

}

然后,随着高于 class 我可以解决这样的元素:

Then, with the above class I am able to work around with the elements like this:

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

但是问题是我无法找到一种方法获取设置 17.5%非-Recoverable 来自上述 XML 示例的 MixedContent-ComplexType 的文本。

But the problem is that I can't found a way to get or set the 17.5% Non-Recoverable text of the MixedContent-ComplexType from the above XML example.

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


  • 使用 mixed =true属性如下:

  • Used mixed="true" attribute like this:

< xsd:complexType name =TaxDescriptionTypemixed =true>

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

(I think XJC is ignoring the last attribute)

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

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

但我不确定这是否是解决这个问题的方法。其中一个答案说这是一个错误,另一个答案显示了一个代码,将 MixedContent 转换为列表< Serializable> 也许接下来的情况将是如何解决这个问题:

But I am not sure if this is the way to solve this. One of the answers said that this is a bug and in the other one shows a code that transforms the MixedContent into a List<Serializable> and maybe the next situation will be about how to deal with this:

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

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

(And I really don't know how to deal with a Serializable element)

推荐答案

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

As you mentioned you need to add the mixed attribute to indicate that your type supports mixed content. Without this specified your XML content is invalid:

<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 类将具有以下内容属性。从本质上讲,这意味着所有非属性内容都将存储在列表中。这是必要的,因为您需要一种机制来指示文本节点与元素内容的位置。

The generated TaxDescriptionType class will have the following property. Essentially this means that all of the non-attribute content will be stored in a List. This is necessary because you need a mechanism that indicates where the text nodes are wrt the element content.

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

您将使用 String (表示文本节点)和 JAXBElement (表示元素内容)。

You will populate this list with instances of String (representing text nodes) and JAXBElement (representing element content).

替代

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

Mixed content generally makes life more complicated than it needs to be. If possible I would recommend an alternate XML representation.

<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>

这篇关于如何使用MixedContent数据处理JAXB ComplexType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆