gpt4 book ai didi

java - 删除 xsi :type after marshalling abstract class with hierarchy

转载 作者:行者123 更新时间:2023-11-29 07:42:01 25 4
gpt4 key购买 nike

如果有人帮助我,那就太好了。所以我有一个结构:

<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>

对于这个结构,我编写了 Java 类,他们对其进行编码和解码:A类 包 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)信件:

@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;
}
}

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;
}
}

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;
}
}

一切看起来都很好,但是在编码之后我是这样的:

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();
}
}

我得到这样的输出:

<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>

问题:如何删除每个列表标签中的 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:type="b"?我知道 JAXB 会通知我它使用的是什么类型,因为最初映射使用的是另一种类型 (A.class),但我不需要该信息。有人有什么想法吗?

最佳答案

如果您在 list 属性上使用 @XmlElementRef,则该项目的元素将基于引用类的 @XmlRootElement然后你将不会获得 xsi:type 属性。

@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;
}
}

关于java - 删除 xsi :type after marshalling abstract class with hierarchy,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29131954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com