java 如何使用 MixedContent 数据处理 JAXB ComplexType?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12568247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to deal with JAXB ComplexType with MixedContent data?
提问by Oscar Jara
I got this XML structure:
我得到了这个 XML 结构:
<Tax>
<Money currency="USD">0.00</Money>
<Description xml:lang="en">
17.5% Non-Recoverable
<ShortName>vatspecial</ShortName>
</Description>
</Tax>
Notice that Description
node has MixedContent
(composed with text and XML)and this is the XSD
part regarding Description
node:
请注意,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>
Everything is ok at this point, XJC
outputs the generated classes like this one regarding TaxDescriptionType
:
此时一切正常,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;
}
}
Then, with the above class
I am able to work around with the elements like this:
然后,有了上面的内容,class
我就可以处理这样的元素:
taxDescriptionType.setLang("en");
taxDescriptionType.setShortName("vatspecial");
/* missing value: 17.5% Non-Recoverable */
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.
但问题是,我不能找到一种方法,get
或者set
将17.5% Non-Recoverable
文本MixedContent-ComplexType
从上面的XML
例子。
This is what I tried and it's not working:
这是我尝试过的,但不起作用:
- Used
mixed="true"
attribute like this:
- 使用的
mixed="true"
属性是这样的:
<xsd:complexType name="TaxDescriptionType" mixed="true">
<xsd:complexType name="TaxDescriptionType" mixed="true">
(I think XJC is ignoring the last attribute)
(我认为 XJC 忽略了最后一个属性)
Doing some research, I found this:
做了一些研究,我发现了这个:
JAXB XJC compiler disregarding mixed=true on XML Schema documents
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:
但我不确定这是否是解决这个问题的方法。一个答案说这是一个错误,而另一个答案显示了一个将 theMixedContent
转换为 a的代码,List<Serializable>
也许下一个情况将是如何处理这个问题:
taxDescriptionType.getContent().add(Serializable element);
(And I really don't know how to deal with a Serializable
element)
(而且我真的不知道如何处理一个Serializable
元素)
采纳答案by bdoughan
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:
正如您所提到的,您需要添加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>
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.
生成的TaxDescriptionType
类将具有以下属性。从本质上讲,这意味着所有非属性内容都将存储在List
. 这是必要的,因为您需要一种机制来指示文本节点与元素内容的位置。
@XmlElementRef(name = "ShortName", namespace = "http://www.example.org/schema", type = JAXBElement.class)
@XmlMixed
protected List<Serializable> content;
You will populate this list with instances of String
(representing text nodes) and JAXBElement
(representing element content).
您将使用String
(代表文本节点)和JAXBElement
(代表元素内容)的实例填充此列表。
ALTERNATIVELY
或者
Mixed content generally makes life more complicated than it needs to be. If possible I would recommend an alternate XML representation.
混合内容通常会使生活变得比实际需要的更复杂。如果可能,我会推荐一个替代的 XML 表示。
<Tax>
<Money currency="USD">0.00</Money>
<Description xml:lang="en" ShortName="vatspecial">
17.5% Non-Recoverable
</Description>
</Tax>
Or
或者
<Tax>
<Money currency="USD">0.00</Money>
<Description xml:lang="en">
<LongName>17.5% Non-Recoverable</LongName>
<ShortName>vatspecial</ShortName>
</Description>
</Tax>
回答by x545
With mixed=true, in ObjectFactory there should be a function like JAXBElement<ShortNameType> createTaxDescriptionTypeShortNameType(ShortNameType)
, which generates the serializable element for you.
使用mixed=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);
}