此时你可能已经解决了这个问题,这是一个有趣的练习,我有一些空闲时间。
在 com.quick 包中创建一个名为 StepByStep 的类,在继续之前删除所有 IDE 生成的代码(清空文件)。
先重新添加包
package com.quick;
首先添加导入
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.w3c.dom.Element;
现在,添加一些类
class Player {List<Property> property = new ArrayList<>();}
class Property {
String id, name, value;
public Property(String id, String name, String value) {
this.id = id;
this.name = name;
this.value = value;
}
}
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD) // or write the get and set methods
class Players {
@XmlJavaTypeAdapter(PlayerTypeAdapter.class) // dont worry about this line
List<Player> player;
}
现在,添加 XmlAdapter 类(PlayerTypeAdapter),这是最重要的部分,如果您已经知道 @XmlJavaTypeAdapter 是如何工作的并且您只想知道如何将任意 xml 放入元素焦点中
class PlayerTypeAdapter extends XmlAdapter<Object, Player> {
private DocumentBuilder documentBuilder;
public PlayerTypeAdapter() {
try {
documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
} catch (Exception e) {
}
}
@Override
public Player unmarshal(Object v) throws Exception {
Player p = new Player();
NodeList c = ((Element) v).getChildNodes();
for (int i = 0; i < c.getLength(); i++) {
Node n = c.item(i);
if (!(n instanceof Element)) continue;
Element e = (Element) n;
p.property.add(new Property(e.getAttribute("id"),
e.getTagName(), e.getTextContent()));
}
return p;
}
@Override
public Object marshal(Player v) throws Exception {
Document document = documentBuilder.newDocument();
Element root = document.createElement("dummy");
if (v.property != null) for (Property p : v.property) {
Element propertyNode = document.createElement(p.name);
propertyNode.setAttribute("id", p.id);
propertyNode.setTextContent(p.value);
root.appendChild(propertyNode);
}
return root;
}
}
最后添加 StepByStep 类及其 main 方法(只是为了测试我们的代码)
public class StepByStep {
public static void main(String[] args) throws JAXBException {
// context, marshaller and unmarshaller
JAXBContext context = JAXBContext.newInstance(Players.class, Player.class, Property.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Unmarshaller unmarshaller = context.createUnmarshaller();
// lets fill dummy players
Players p = new Players();
p.player = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Player e = new Player();
e.property.add(new Property("001", "FirstName", "Mahendra"));
e.property.add(new Property("002", "MiddleName", "Sing"));
e.property.add(new Property("003", "LastName", "Dhoni"));
p.player.add(e);
}
// marshal p (original)
ByteArrayOutputStream os1 = new ByteArrayOutputStream();
marshaller.marshal(p, os1);
byte[] ba1 = os1.toByteArray();
Players q = (Players) unmarshaller.unmarshal(new ByteArrayInputStream(ba1));
// marshal q (copy)
ByteArrayOutputStream os2 = new ByteArrayOutputStream();
marshaller.marshal(q, os2);
byte[] ba2 = os2.toByteArray();
// both q and p should be the same
System.out.println(new String(ba1));
System.out.println(new String(ba2));
}
}