Remove xsi:type after marshalling abstract class with hierarchy
If someone help me, that will be great. So I have a structure:
<letters> <list> <name>Simon</name> <type>2</type> <passengerName>Johny</passengerName> <passengerSurname>Revelator</passengerSurname> </list> <list> <name>someName</name> <type>4</type> <fileURL>someUrl</fileURL> <specialNotes>specialNotes</specialNotes> </list> </letters>
For this structure I wrote Java Classes and they marshall and unmarshall it: Class A package com.edhex.testing;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.PROPERTY)
@XmlSeeAlso({B.class, C.class})
@XmlType
abstract public class A {
int type;
String name;
@XmlElement
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
@XmlElement
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Class Letters:
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Letters {
List<A> list;
@XmlElement
public List<A> getList() {
return list;
}
public void setList(List<A> list) {
this.list = list;
}
}
Class B:
@XmlRootElement
@XmlType
@XmlAccessorType(XmlAccessType.PROPERTY)
public class B extends A {
String fileURL;
String specialNotes;
@XmlElement
public String getFileURL() {
return fileURL;
}
public void setFileURL(String fileURL) {
this.fileURL = fileURL;
}
@XmlElement
public String getSpecialNotes() {
return specialNotes;
}
public void setSpecialNotes(String specialNotes) {
this.specialNotes = specialNotes;
}
}
Class C:
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class C extends A {
String passengerName;
String passengerSurname;
@XmlElement
public String getPassengerName() {
return passengerName;
}
public void setPassengerName(String passengerName) {
this.passengerName = passengerName;
}
@XmlElement
public String getPassengerSurname() {
return passengerSurname;
}
public void setPassengerSurname(String passengerSurname) {
this.passengerSurname = passengerSurname;
}
}
Everything looks fine, but after marshalling which I do like this:
public static void main(String[] args) {
C c = new C();
c.setName("Simon");
c.setType(2);
c.setPassengerName("Johny");
c.setPassengerSurname("Revelator");
B b = new B();
b.setType(4);
b.setFileURL("someUrl");
b.setSpecialNotes("specialNotes");
b.setName("someName");
List<A> list = new ArrayList<A>(2);
list.add(c);
list.add(b);
Letters letter = new Letters();
letter.setList(list);
try {
File file = new File("C:file.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Letters.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(letter, file);
jaxbMarshaller.marshal(letter, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
I get output like this:
<letters>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="c">
<name>Lasha</name>
<type>2</type>
<passengerName>Johny</passengerName>
<passengerSurname>Revelator</passengerSurname>
</list>
<list xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b">
<name>someName</name>
<type>4</type>
<fileURL>someUrl</fileURL>
<specialNotes>specialNotes</specialNotes>
</list>
</letters>
Question: How can I remove those: xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="b" in each list tag? I know that JAXB will notify me with what type is it using, because originally mapping is using another type (A.class), but I don't need that information. Have somebody any ideas?
如果在list
属性上使用@XmlElementRef
,则该项目的元素将基于被引用类的@XmlRootElement
,然后您将不会获得xsi:type
attibute。
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Letters {
List<A> list;
@XmlElementRef
public List<A> getList() {
return list;
}
public void setList(List<A> list) {
this.list = list;
}
}
looks like you can use generics - to keep the types generic: Remove xsi:type, xmlns:xs, and xmlns:xsi from JAXB Generics
But my answer is - do not remove the xmlns:xsi declaration. I know it would look nicer to have it in the top element. But - this way it is still valid XML with valid and complete type identification. The XML is meant to be read by machines. Without the xsi:type attribute the mapping between objects and XML wouldn't be deterministic anymore (and you couldn't as well validate the XML reliably).
Have fun.. Gabriel
链接地址: http://www.djcxy.com/p/51722.html上一篇: 根元素和元素参考名称问题的命名空间