我正在尝试使用 JAXB 将带有两个 @XmlJavaTypeAdapter 注释(订阅者、标签)的数据(多边形)解组到 XML 文件中。每个对象都有它自己的 XML 文件,并且这些对象是相关的:
- 每个订阅者可以持有多个多边形
- 每个多边形可以拥有多个订阅者
- 每个多边形可以包含多个标签
我正在尝试将 Polygon XML 文件解组回其类对象,其订阅者和标签都完好无损,但无法弄清楚如何去做。
== 订阅者.java ==
@XmlRootElement(name = "Subscriber")
@XmlAccessorType(XmlAccessType.FIELD)
public class Subscriber {
private String ID;
private Set<Polygon> polygons;
@XmlAttribute(name = "ID")
public String getID() {
return this.ID;
}
public void setID(String ID) {
this.ID = ID;
}
@XmlJavaAdapter(PolygonXmlAdapter.class)
@XmlElementWrapper(name = "Polygons")
@XmlElement(name = "Polygon")
public set<Polygon> getPolygons() {
return this.polygons;
}
public void setPolygons(Set<Polygon> polygons) {
this.polygons = polygons;
}
}
==多边形.java ==
@XmlRootElement(name = "{Polygon}")
@XmlAccessorType(XmlAccessType.FIELD)
public class Polygon {
private String ID;
private Set<Subscriber> subscribers;
private Set<Tag> tags
@XmlAttribute(name = "ID")
public String getID() {
return this.ID;
}
public void setID(String ID) {
this.ID = ID;
}
@XmlJavaAdapter(SubscriberXmlAdapter.class)
@XmlElementWrapper(name = "Subscribers")
@XmlElement(Subscriber)
public Set<Subscriber> getSubscribers() {
return this.subscribers;
}
@XmlJavaAdapter(TagXmlAdapter.class)
@XmlElementWrapper(name = "Tags")
@XmlElement(name = "Tag")
public Set<Tag> getTags() {
return this.tags
}
public void setTags(Set<Tag> tags) {
this.tags = tags;
}
}
== 标签.java ==
@XmlRootElement(name = "Tag")
@XmlAccessorType(XmlAccessType.FIELD)
public class Tag {
private String ID;
@XmlAttribute(name = "ID")
public String getID() {
return this.ID;
}
public void setID(String ID) {
this.ID = ID;
}
}
== 订阅者XmlAdapter.java ==
public class SubscriberXmlAdapter extends XmlAdapter<String, Subscriber> {
private Map<String, Subscriber> subscribers = new HashMap<>();
private Map<String, Subscriber> getSubscribers() {
return subscribers;
}
@Override
public Subscriber unmarshal(String v) throws Exception {
return subscribers.get(v);
}
@Override
public String marshal(Subscriber v) throws Exception {
return v.getID();
}
}
== TagXmlAdapter.java ==
public class TagXmlAdapter extends XmlAdapter<String, Tag> {
private Map<String, Tag> tags = new HashMap<>();
private Map<String, Tag> getTags() {
return tags;
}
@Override
public String unmarshal(String v) throws Exception {
return tags.get(v);
}
@Override
public String marshal(Tag v) throws Exception {
reutrn v.getID();
}
}
== 订阅者集合.java ==
@XmlRootElement(name = "Subscribers")
@XmlAccessorType(XmlAccessType.FIELD)
public class SubscriberCollection {
private Collection<Subscriber> collection;
@XmlElement(name = "Subscriber")
public Collection<Subscriber> getSubscribers() {
return this.collection;
}
public void setCollection(Collection<Subscriber> collection) {
this.collection = collection;
}
}
== TagCollection.java ==
@XmlRootElement(name = "Tags")
@XmlAccessorType(XmlAccessType.FIELD)
public class TagCollection {
private Collection<Subscriber> collection;
@XmlElement(name="Tag")
public Collection<Tag> getSubscribers() {
return this.collection;
}
public void setCollection(Collection<Subscriber> collection) {
this.collection = collection;
}
}
为了执行解组,使用了三个 xml 文件 - 用于订阅者、多边形和标签 - 包含它们各自的数据:
JAXBContext jaxbContext = JAXBContext.newInstance(SubscriberCollection.class, PolygonTagCollection.class, Polygon.class);
unmarshaller subUnmarshaller = jaxbContext.createunmarshaller();
SubscriberCollection subCollection = (SubscriberCollection) subUnmarshaller.unmarshal(new ByteArrayInputStream(subscribersXml.getBytes(StandardCharset.UTF_8.name())));
Unmarshaller tagUnmarshaller = jaxbContext.createUnmarshaller();
TagCollection tagCollectino = (TagCollection) tagUnmarshaller.un,arshal(new ByteArrayInputStream(tagXml.getBytes(StandardCharsets.UTF_8.name())));
Unmarshaller polUnmarshaller = jaxbContext.createUnmarshaller();
SubscriberXmlAdapter subAdapter = new SubscriberXmlAdapter();
for (Subscriber sub : subscriberCollection.getCollection()) {
subAdapter.getCollection().put(sub.getID(), sub);
}
TagXmlAdapter tagAdapter = new TagXmlAdapter();
for (Tag tag : tagCollection.getCollection()) {
tagAdapter.getCollection().put(tag.getID(), tag);
}
polUnmarshaller.setAdapter(SubscriberXmlAdapter.class subAdapter);
polUnmarshaller.unmarshal(new ByteArrayInputStream(polygonXml.getBytes(StandardCharsets.UTF_8.name())));
这将使用正确的订阅者解组 Polygon,但不使用正确的标签。
我只能将一个 XmlAdapter 设置为解组器,因此它只能填充正在解组的多边形内的订阅者或标签。
有没有办法组合这些适配器?或者有没有更好的方法来做事。
谢谢!