簡體   English   中英

如何處理帶有MixedContent數據的JAXB ComplexType?

[英]How to deal with JAXB ComplexType with MixedContent data?

我得到了以下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;
    }

}

然后,通過上面的class我可以處理以下元素:

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

但是問題是,我找不到從上述XML示例中getset MixedContent-ComplexType17.5% Non-Recoverable文本的方法。


這是我嘗試過的,但是不起作用:

  • 使用如下的mixed="true"屬性:

<xsd:complexType name="TaxDescriptionType" mixed="true">

(我認為XJC忽略了最后一個屬性)


做一些研究,我發現:

JAXB XJC編譯器忽略XML Schema文檔上的mixed = true

但是我不確定這是否是解決問題的方法。 答案之一表示這是一個錯誤,而另一答案則顯示了將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>

如果為blend = true,則在ObjectFactory中應該有一個類似於JAXBElement<ShortNameType> createTaxDescriptionTypeShortNameType(ShortNameType)的函數,該函數會為您生成可序列化的元素。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM