我正在研究配置XML文件,我希望它是动态的。
我有以下课程:
存储库:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "repository")
public class Repository
implements Serializable
{
@XmlElement(name = "custom-configuration")
private CustomConfiguration customConfiguration;
...
// Constructor
// Getters and setters
}
CustomConfiguration:
@XmlAccessorType(XmlAccessType.FIELD)
public class CustomConfiguration
{
}
FooBarConfiguration:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "foo-bar-configuration")
public class FooBarConfiguration extends CustomConfiguration
{
@XmlAttribute
private String bucket;
@XmlAttribute
private String key;
public FooBarConfiguration()
{
}
public String getBucket()
{
return bucket;
}
public void setBucket(String bucket)
{
this.bucket = bucket;
}
public String getKey()
{
return key;
}
public void setKey(String key)
{
this.key = key;
}
}
这就是我想要的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<repository id="test-repository">
<foo-bar-configuration bucket="test-bucket" key="test-key"/>
<group/>
</repository>
这就是我所得到的:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<repository id="test-repository">
<customConfiguration xsi:type="fooBarConfiguration" bucket="test-bucket" key="test-key" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<group/>
</repository>
任何想法如何实现这一目标?
答案 0 :(得分:1)
您应该能够使用@XmlElementRef
按元素名称编组,例如:
@XmlElementRef
private CustomConfiguration customConfiguration;
以下是Blaise Doughan的一个例子:http://blog.bdoughan.com/2010/11/jaxb-and-inheritance-using-substitution.html
Javadoc:https://docs.oracle.com/javase/8/docs/api/javax/xml/bind/annotation/XmlElementRef.html
答案 1 :(得分:1)
根据要求,这是一个真实的例子。
某些基本类型的Schema declares and abstract element:
<xsd:element name="AbstractQueryExpression"
type="fes:AbstractQueryExpressionType" abstract="true"/>
<xsd:complexType name="GetPropertyValueType">
<xsd:complexContent>
<xsd:extension base="wfs:BaseRequestType">
<xsd:sequence>
<xsd:element ref="fes:AbstractQueryExpression"/>
</xsd:sequence>
<xsd:attribute name="valueReference" type="xsd:string"
use="required"/>
<xsd:attribute name="resolvePath" type="xsd:string"/>
<xsd:attributeGroup ref="wfs:StandardPresentationParameters"/>
<xsd:attributeGroup ref="wfs:StandardResolveParameters"/>
</xsd:extension>
</xsd:complexContent>
</xsd:complexType>
这导致以下Java代码:
@XmlElementRef(name = "AbstractQueryExpression", namespace = "http://www.opengis.net/fes/2.0", type = JAXBElement.class)
protected JAXBElement<?> abstractQueryExpression;
Another schema declares an element which may substitute the original element:
<xsd:element name="StoredQuery" type="wfs:StoredQueryType"
substitutionGroup="fes:AbstractQueryExpression"/>
在ObjectFactory
中,如下所示:
@XmlElementDecl(namespace = "http://www.opengis.net/wfs/2.0", name = "StoredQuery", substitutionHeadNamespace = "http://www.opengis.net/fes/2.0", substitutionHeadName = "AbstractQueryExpression")
public JAXBElement<StoredQueryType> createStoredQuery(StoredQueryType value) {
return new JAXBElement<StoredQueryType>(_StoredQuery_QNAME, StoredQueryType.class, null, value);
}
此机制可用于实现&#34;扩展点&#34;。您声明并使用抽象/通用&#34;替换头&#34;元素和其他模式中的特定元素可以替代它。
希望这有帮助。