[英]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>
* <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
示例中get
或set
MixedContent-ComplexType
的17.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.