我得到了這個XML的結構:如何使用MixedContent數據處理JAXB ComplexType?
<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.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>
* </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
以上的XML
例子的get
或set
17.5% Non-Recoverable
MixedContent-ComplexType
的文本。
這是我嘗試和它不工作:
- 使用
mixed="true"
屬性是這樣的:
<xsd:complexType name="TaxDescriptionType" mixed="true">
(我認爲XJC是忽略l AST屬性)
做一些研究,我發現這一點:
JAXB XJC compiler disregarding mixed=true on XML Schema documents
但我不知道這是解決這個問題的方法。答案之一說,這是一個錯誤,並在另一個顯示,變換MixedContent
爲List<Serializable>
,也許接下來的情況會如何處理這個代碼:
taxDescriptionType.getContent().add(Serializable element);
(和我真的不知道該如何應對Serializable
元素)
「List」的'Serializable'類型參數是重要的嗎? –