Osc*_*car 8 java xml xsd jaxb xjc
我有这个XML结构:
<Tax>
<Money currency="USD">0.00</Money>
<Description xml:lang="en">
17.5% Non-Recoverable
<ShortName>vatspecial</ShortName>
</Description>
</Tax>
Run Code Online (Sandbox Code Playgroud)
请注意,Description节点具有MixedContent (由文本和XML组成),这是XSD与Description节点有关的部分:
<xsd:complexType name="TaxDescriptionType">
<xsd:sequence>
<xsd:element name="ShortName" type="xsd:string" />
</xsd:sequence>
<xsd:attribute ref="xml:lang" />
</xsd:complexType>
Run Code Online (Sandbox Code Playgroud)
此时一切正常,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;
}
}
Run Code Online (Sandbox Code Playgroud)
然后,通过上面class我可以解决这样的元素:
taxDescriptionType.setLang("en");
taxDescriptionType.setShortName("vatspecial");
/* missing value: 17.5% Non-Recoverable */
Run Code Online (Sandbox Code Playgroud)
但问题是,我不能找到一种方法,get或者set将17.5% Non-Recoverable文本MixedContent-ComplexType从上面的XML例子.
这是我尝试过的,它不起作用:
mixed="true"属性如下:<xsd:complexType name="TaxDescriptionType" mixed="true">
(我认为XJC忽略了最后一个属性)
做一些研究,我发现了这个:
JAXB XJC编译器忽略XML Schema文档中的mixed = true
但我不确定这是否是解决这个问题的方法.答案之一说,这是一个错误,并在另一个显示,变换代码MixedContent到List<Serializable>,也许在未来情况会如何处理这个:
taxDescriptionType.getContent().add(Serializable element);
Run Code Online (Sandbox Code Playgroud)
(我真的不知道如何处理一个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>
Run Code Online (Sandbox Code Playgroud)
生成的TaxDescriptionType类将具有以下属性。从本质上讲,这意味着所有非属性内容都将存储在List. 这是必要的,因为您需要一种机制来指示文本节点与元素内容的位置。
@XmlElementRef(name = "ShortName", namespace = "http://www.example.org/schema", type = JAXBElement.class)
@XmlMixed
protected List<Serializable> content;
Run Code Online (Sandbox Code Playgroud)
您将使用String(表示文本节点)和JAXBElement(表示元素内容)的实例填充此列表。
或者
混合内容通常会使生活变得比实际需要的更复杂。如果可能,我会推荐一个替代的 XML 表示。
<Tax>
<Money currency="USD">0.00</Money>
<Description xml:lang="en" ShortName="vatspecial">
17.5% Non-Recoverable
</Description>
</Tax>
Run Code Online (Sandbox Code Playgroud)
或者
<Tax>
<Money currency="USD">0.00</Money>
<Description xml:lang="en">
<LongName>17.5% Non-Recoverable</LongName>
<ShortName>vatspecial</ShortName>
</Description>
</Tax>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
12148 次 |
| 最近记录: |