Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 接口和jaxb_Java_Xml_Interface_Jaxb - Fatal编程技术网

Java 接口和jaxb

Java 接口和jaxb,java,xml,interface,jaxb,Java,Xml,Interface,Jaxb,我将向您展示我的代码,它将更加清晰: @XmlRootElement FilePollerConf{ ArrayList<Directory> directoriesList = new ArrayList<Directory>(); } Directory{ ArrayList<Match> matchList = new ArrayList<Match>(); } Match{ ArrayList<E

我将向您展示我的代码,它将更加清晰:

@XmlRootElement
FilePollerConf{

    ArrayList<Directory> directoriesList = new ArrayList<Directory>();

}

Directory{

    ArrayList<Match> matchList = new ArrayList<Match>();

}

Match{

    ArrayList<Event> eventsList = new ArrayList<Event>();

}

Event{

    ArrayList<IAction> actionsList = new ArrayList<IAction>();

}

IAction{

    void send();

}
@XmlRootElement
FilePollerConf{
ArrayList directoriesList=新的ArrayList();
}
目录{
ArrayList匹配列表=新的ArrayList();
}
匹配{
ArrayList eventsList=新的ArrayList();
}
事件{
ArrayList actionsList=新建ArrayList();
}
IAction{
无效发送();
}
这里是要点,当我尝试用jaxb解组时,我有一个错误:

IAction是一个接口,JAXB不能处理接口

所以我寻找@XmlAdapter,但我没有看到像我这样的用例,所以我真的不知道我是否可以使用它?我会继续搜索,但如果你有什么想法,欢迎你!事实上,我已经有了我的xml,我希望jaxb生成我的xml(提供xml):


toto.com
21
托托
提提
/根/src
邮寄
smtp.fr.gric
25
toto@rock.com
titi@rock.com
塔塔
布拉布拉
ergrthrhdrth
麦尔盖格
toto.sgcib.com
21
托托
提提
/根/src
C://Desktop/privatekey.prk

关键是我的结构并不总是相同的,所以我可以使用jaxb吗?

您可以使用
@XmlElement
注释来指定接口字段/属性的实现类型

@XmlAccessorType(XmlAccessType.FIELD)
public class Event{

    @XmlElement(type=ActionImpl.class)
    ArrayList<IAction> actionsList = new ArrayList<IAction>();

}
@xmlacessortype(xmlacesstype.FIELD)
公开课活动{
@XmlElement(类型=ActionImpl.class)
ArrayList actionsList=新建ArrayList();
}
了解更多信息


我不确定它是否有效,但您是否尝试使用抽象类而不是接口


我想知道JaxB需要实例化类来进行转换…

我知道可以使用
@xmlanyement
来避免对包含接口作为元素的类进行编组和解编组时出错。这导致了一个缺点,即在解组时的类型。如果使用
@xmlanyement(lax=true)
并在具体类上指定具有特定名称的
@XmlRootElement
,或者在要解组的元素上指定xsi:type,那么它将完全解组到具体类。我不知道如何做后者,但前者使用起来非常简单(
@XmlRootElement(name=“my-concrete-element”)

例如:

公共接口摘要{
公共void setData(字符串数据);
公共字符串getData();
}
@XmlRootElement(name=“concrete1”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类Concrete1实现抽象{
@XmlElement
私有字符串数据;
@凌驾
公共void setData(字符串数据){
这个数据=数据;
}
@凌驾
公共字符串getData(){
返回此.data;
}
@凌驾
公共字符串toString(){
返回“@Concrete1{data:+this.data+“}”;
}
}
@XmlRootElement(name=“concrete2”)
@XmlAccessorType(XmlAccessType.FIELD)
公共类2实现抽象{
@XmlElement
私有字符串数据;
@凌驾
公共void setData(字符串数据){
这个数据=数据;
}
@凌驾
公共字符串getData(){
返回此.data;
}
@凌驾
公共字符串toString(){
返回“@Concrete2{data:+this.data+“}”;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
公共类根{
@XmlElement
私人弦道具;
@XmlElementWrapper(name=“abstracts”)
@xmlanyement(lax=true)
私人ArrayList摘要;
公共无效集合摘要(ArrayList摘要){
this.abstracts=摘要;
}
公共ArrayList getAbstracts(){
返回摘要;
}
@凌驾
公共字符串toString(){
String ret=“{prop:+this.prop+”,摘要:[”;
Object[]abstracts=this.abstracts.toArray();
for(对象摘要:摘要){
ret+=abstract.toString()+“,”;
}
返回ret+“]}”;
}
公共无效设置属性(字符串属性){
this.prop=prop;
}
公共字符串getProp(){
返回道具;
}
}

如果有人知道如何在不使用任何硬编码解决方案的情况下强制xsi:type出现在具体类中,我将非常感谢您分享解决方案。

尝试将IAction转换为抽象类(或添加实现IAction的抽象类)。这对我来说很有用,但我不知道是否适合你的设计。非常感谢,但我不明白的是,我的接口IAAction将有多个实现,它可能是一个具有四种不同类型的列表,因此我无法编写@XmlElement(type=FTPSend.class)@XmlElement(type=MailSend.class)@XmlElement(type=ServerToServerSend.class)?@JulienMoreau-您可以将
@xmlement
的多个实例包装在
@xmlements
注释中。@xmlements(value={@xmlement(type=FTPSend.class)、@xmlement(type=SFTPSend.class)、@xmlement(type=ServerToServerSend.class)})ArrayList ActionList=new ArrayList(); 这条路看起来有用!谢谢你救了我!上帝刚刚看到你在渥太华工作,我去年在这里,我梦想着在去科克的路上回来,住在加拿大!试着知道你有多大的机会@JulienMoreau-渥太华绝对是一个居住/工作的好城市。渥太华的就业市场最近似乎正在复苏。
@XmlAccessorType(XmlAccessType.FIELD)
public class Event{

    @XmlElement(type=ActionImpl.class)
    ArrayList<IAction> actionsList = new ArrayList<IAction>();

}