我正在处理一个配置XML文件,我希望它是动态的。JAXB继承动态命名标签
我有以下类别:
库:
@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>
任何想法如何实现这一目标?
问题是我没有使用XSD。我有类可以扩展未知的CustomConfiguration。他们可能或不在类路径中,所以我不能只写一个容易的ObjectMapper(因为我必须事先知道这些类的名字)。还有其他建议吗? – carlspring
@carlspring你不需要XSD,也不需要事先知道类。将'ObjectFactory'放在你的扩展类所在的位置。构建JAXB上下文时在运行时使用它。编译时无需知道这些类。 – lexicore
您是否认为您可以快速浏览我的差异:https://github.com/strongbox/strongbox/pull/183/files并告诉我我做错了什么? (如果您想构建代码,请点击此处:https://github.com/strongbox/strongbox/wiki/Building-the-code)。这是我得到的错误:https://github.com/strongbox/strongbox/pull/183/commits/9bdc666c32e7ba04df2e1befe7655050c1652bc9。 – carlspring