Ser*_*gio 8 java xml jaxb jaxb2
这是我的情景.我有一个通用类:
public class Tuple<T> extends ArrayList<T> {
//...
public Tuple(T ...members) {
this(Arrays.asList(members));
}
@XmlElementWrapper(name = "tuple")
@XmlElement(name = "value")
public List<T> getList() {
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
和一个儿童班:
public class StringTuple extends Tuple<String> {
public StringTuple(String ...members) {
super(members);
}
//explanation of why overriding this method soon ...
@XmlElementWrapper(name = "tuple")
@XmlElement(name = "value")
@Override
public List<String> getList() {
return this;
}
}
Run Code Online (Sandbox Code Playgroud)
这里引用了这些类:
@XmlRootElement(namespace = "iv4e.xml.jaxb.model")
public class Relation {
private Tuple<StringTuple> relationVars;
//...
@XmlElementWrapper(name = "allRelationVars")
@XmlElement(name = "relationVarsList")
public Tuple<StringTuple> getRelationVars() {
return relationVars;
}
}
Run Code Online (Sandbox Code Playgroud)
然后使用以下内容创建Relation对象:
Relation rel = new Relation();
rel.setRelationVars(new Tuple<StringTuple>(
new StringTuple("RelationshipVar1"), new StringTuple("RelationshipVar2")));
Run Code Online (Sandbox Code Playgroud)
编组此对象后,Xml输出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:relation xmlns:ns2="iv4e.xml.jaxb.model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="">
<allRelationVars>
<relationVarsList>
<tuple>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">RelationshipVar1</value>
</tuple>
<tuple>
<value>RelationshipVar1</value>
</tuple>
</relationVarsList>
<relationVarsList>
<tuple>
<value xmlns:xs="http://www.w3.org/2001/XMLSchema" xsi:type="xs:string">RelationshipVar2</value>
</tuple>
<tuple>
<value>RelationshipVar2</value>
</tuple>
</relationVarsList>
</allRelationVars>
</ns2:relation>
Run Code Online (Sandbox Code Playgroud)
所以value元素是重复的!
现在,类StringTuple覆盖的原因List<T> getList()与List<String> getList()被避免烦人生成xmlns:xs列表中的每个成员的属性(在valueXML文档中的元素).但是然后列表中的每个成员在输出中显示两次.显然,这是因为重写的父方法和子方法都注释了@XmlElement.所以我的主要问题是:有一种方法可以忽略@XmlElement在Jaxb中注释的重写方法吗?(考虑到覆盖方法也注明了@XmlElement)
我发现一个旧帖子报告了类似的问题:http://old.nabble.com/@XmlElement-on-overridden-methods-td19101616.html,但我还没有找到任何解决方案.另请注意,在父类()中@XmlTransient为getList方法添加注释Tuple<T>可以解决此问题,但会生成其他问题,因为父类不是抽象的,并且在其他上下文中单独使用.
一方面的第二个问题:是否有可能xmlns:xs在根节点声明属性而不是它 - 令人不快 - 出现在需要它的每个节点中?我知道这可以通过NamespacePrefixMapper类完成,但由于它是一个非标准的SUN内部类,我宁愿使用更多独立于实现的方法.
提前感谢您的任何反馈!
@XmlTransient您可以使用以下方法在父级和@XmlElement子级上标记属性:
家长
package forum7851052;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
@XmlRootElement
public class Parent<T> {
private List<T> item = new ArrayList<T>();
@XmlTransient
public List<T> getItem() {
return item;
}
public void setItem(List<T> item) {
this.item = item;
}
}
Run Code Online (Sandbox Code Playgroud)
整数子代
package forum7851052;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class IntegerChild extends Parent<Integer> {
@Override
@XmlElement
public List<Integer> getItem() {
return super.getItem();
}
@Override
public void setItem(List<Integer> item) {
super.setItem(item);
}
}
Run Code Online (Sandbox Code Playgroud)
字符串子对象
package forum7851052;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class StringChild extends Parent<String> {
@Override
@XmlElement
public List<String> getItem() {
return super.getItem();
}
@Override
public void setItem(List<String> item) {
super.setItem(item);
}
}
Run Code Online (Sandbox Code Playgroud)
演示
package forum7851052;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Parent.class, IntegerChild.class, StringChild.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
IntegerChild integerChild = new IntegerChild();
integerChild.getItem().add(1);
integerChild.getItem().add(2);
marshaller.marshal(integerChild, System.out);
StringChild stringChild = new StringChild();
stringChild.getItem().add("A");
stringChild.getItem().add("B");
marshaller.marshal(stringChild, System.out);
}
}
Run Code Online (Sandbox Code Playgroud)
输出
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<integerChild>
<item>1</item>
<item>2</item>
</integerChild>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<stringChild>
<item>A</item>
<item>B</item>
</stringChild>
Run Code Online (Sandbox Code Playgroud)