注意:我有保密协议(protocol),所以我必须使用没有意义的变量名称。抱歉
你好! 我有一个抽象类:
public abstract class A {
public abstract List<String> someMethod();
}
以及两个扩展该抽象类的类:
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@XmlRootElement(name = "BField")
@XmlAccessorType(XmlAccessType.FIELD)
public class B extends A {
...
}
@Data
@EqualsAndHashCode(callSuper = true)
@ToString(callSuper = true)
@XmlRootElement(name = "CField")
@XmlAccessorType(XmlAccessType.FIELD)
public class C extends A {
...
}
然后在代码中的某个位置有一个类,它列出了这两个类的列表。几乎看起来像这样:
@XmlElementWrapper(name = "MyStuffData")
@XmlAnyElement(lax = true)
@XmlElementRefs({
@XmlElementRef(name = "B", type = B.class),
@XmlElementRef(name = "C", type = C.class),
})
private List<A> myStuff;
myStuff
的内容可以是 B 或 C,对吗?我如何迭代该列表?我正在从肥皂请求中获取数据,所以我对如何执行此操作感到非常困惑。我尝试过:
for(A a: myStuff){
...some code here...
}
但是我遇到了这个异常:
Caused by: java.lang.ClassCastException: org.apache.xerces.dom.ElementNSImpl cannot be cast to some.package.here.A
听说 lax=true 可以解决问题,但我仍然遇到同样的错误。令人困惑的是,我可以获得列表的大小,这是非常准确的,但我无法迭代它。任何帮助,将不胜感激。 抱歉,我不太擅长解释,所以在谷歌上找到这类东西有点痛苦。
最佳答案
对于此用例,您不应使用@XmlAnyElement(lax=true)
。
用例 #1 - @XmlElementRef
使用@XmlElementRef
时,您需要确保@XmlElementRef
注释上的元素名称与@XmlRootElement
(或 @XmlElementDecl
) 完全注释。
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<MyStuffData>
<BField/>
<CField/>
<BField/>
</MyStuffData>
</root>
根
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElementWrapper(name = "MyStuffData")
@XmlElementRefs({
@XmlElementRef(name = "BField", type = B.class),
@XmlElementRef(name = "CField", type = C.class),
})
private List<A> myStuff;
public List<A> getMyStuff() {
return myStuff;
}
}
<小时/>
用例 #2 - @XmlElements
如果您不希望元素名称与引用类型的根元素名称相同,可以使用@XmlElements
注释。
input.xml
<?xml version="1.0" encoding="UTF-8"?>
<root>
<MyStuffData>
<B/>
<C/>
<B/>
</MyStuffData>
</root>
根
import java.util.List;
import javax.xml.bind.annotation.*;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlElementWrapper(name = "MyStuffData")
@XmlElements({
@XmlElement(name = "B", type = B.class),
@XmlElement(name = "C", type = C.class),
})
private List<A> myStuff;
public List<A> getMyStuff() {
return myStuff;
}
}
<小时/>
常见
以下内容是这两个用例的共同点。
A
public abstract class A {
}
B
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "BField")
public class B extends A {
}
C
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "CField")
public class C extends A {
}
演示
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum17061559/input.xml");
Root root = (Root) unmarshaller.unmarshal(xml);
for(A a : root.getMyStuff()) {
System.out.println(a);
}
}
}
输出
forum17061559.B@6998e5d9
forum17061559.C@351a3fb8
forum17061559.B@4e4d6
关于java - 如何迭代不同对象的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17061559/